|
| 1 | + |
| 2 | +package main |
| 3 | + |
| 4 | +import ( |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | +) |
| 11 | + |
| 12 | +var testCases = []struct { |
| 13 | + a, b, addExpected, subExpected, mulExpected, divExpected int64 |
| 14 | +}{ |
| 15 | + {1, 2, 3, -1, 2, 0}, |
| 16 | + {17, 3, 20, 14, 51, 5}, |
| 17 | + {2147, 107, 2254, 2040, 229729, 20}, |
| 18 | + {39957, 673, 40630, 39284, 26891061, 59}, |
| 19 | + {45571256, 1007, 45572263, 45570249, 45890254792, 45254}, |
| 20 | +} |
| 21 | + |
| 22 | +func TestAdd(t *testing.T) { |
| 23 | + for _, tc := range testCases { |
| 24 | + assert.Equal(t, tc.addExpected, Add(tc.a, tc.b)) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +func TestSub(t *testing.T) { |
| 29 | + for _, tc := range testCases { |
| 30 | + assert.Equal(t, tc.subExpected, Sub(tc.a, tc.b)) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func TestMul(t *testing.T) { |
| 35 | + for _, tc := range testCases { |
| 36 | + assert.Equal(t, tc.mulExpected, Mul(tc.a, tc.b)) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func TestDiv(t *testing.T) { |
| 41 | + for _, tc := range testCases { |
| 42 | + assert.Equal(t, tc.divExpected, Div(tc.a, tc.b)) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func TestE2E(t *testing.T) { |
| 47 | + var err error |
| 48 | + |
| 49 | + r1, w1, _ := os.Pipe() |
| 50 | + r2, w2, _ := os.Pipe() |
| 51 | + |
| 52 | + stdin := os.Stdin |
| 53 | + stdout := os.Stdout |
| 54 | + |
| 55 | + defer func() { |
| 56 | + os.Stdin = stdin |
| 57 | + os.Stdout = stdout |
| 58 | + }() |
| 59 | + |
| 60 | + os.Stdin = r1 |
| 61 | + os.Stdout = w2 |
| 62 | + |
| 63 | + buf := make([]byte, 1024) |
| 64 | + var n int |
| 65 | + |
| 66 | + for _, tc := range testCases { |
| 67 | + input := fmt.Sprintf("%d\n%d\n", tc.a, tc.b) |
| 68 | + expected := fmt.Sprintf("Add: %d\nSubtract: %d\nMultiply: %d\nDivide: %d\n", tc.addExpected, tc.subExpected, tc.mulExpected, tc.divExpected) |
| 69 | + |
| 70 | + if _, err = w1.Write([]byte(input)); err != nil { |
| 71 | + t.Fatal(err) |
| 72 | + } |
| 73 | + main() |
| 74 | + |
| 75 | + if n, err = r2.Read(buf); err != nil { |
| 76 | + t.Fatal(err) |
| 77 | + } |
| 78 | + if n < 70 { |
| 79 | + t.Fatal("Error") |
| 80 | + } |
| 81 | + assert.Equal(t, expected, string(buf[70:n])) |
| 82 | + } |
| 83 | +} |
0 commit comments