-
Notifications
You must be signed in to change notification settings - Fork 1
/
cal_test.go
47 lines (42 loc) · 1.07 KB
/
cal_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"strconv"
"testing"
)
func Test1(t *testing.T) {
expected := "4/3"
result:= answerType(calculate("4 3 / 1 3 / + 2 * 4 - 2 +"))
if expected != result {
t.Errorf("Test fail expected: %s, result: %s\n", expected, result)
}
}
func Test2(t *testing.T) {
expected := "5"
result:= answerType(calculate("1 1 1 1 1 1 + + + +"))
if expected != result {
t.Errorf("Test fail expected: %s, result: %s\n", expected, result)
}
}
func Test3(t *testing.T) {
expected := "1/2"
result:= answerType(calculate("1 3 / 1 6 / +"))
if expected != result {
t.Errorf("Test fail expected: %s, result: %s\n", expected, result)
}
}
func Test5(t *testing.T) {
expected := "1"
result:= answerType(calculate("1 2 / 1 2 / +"))
if expected != result {
t.Errorf("Test fail expected: %s, result: %s\n", expected, result)
}
}
func answerType(answer Fraction) string {
if answer.d == 1 {
return strconv.Itoa(answer.n)
} else if answer.d < 0 {
return strconv.Itoa(-answer.n)+ "/"+strconv.Itoa(-answer.d)
} else {
return strconv.Itoa(answer.n)+ "/"+strconv.Itoa(answer.d)
}
}