Skip to content

Commit 2a121d4

Browse files
abdul-manaanjoncalhoun
authored andcommitted
Solution with unit tests (#51)
1 parent 617e1ce commit 2a121d4

File tree

3 files changed

+203
-0
lines changed

3 files changed

+203
-0
lines changed

students/abdul/problem.csv

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
5+5,10
2+
7+3,10
3+
1+1,2
4+
8+3,11
5+
1+2,3
6+
8+6,14
7+
3+1,4
8+
1+4,5
9+
5+1,6
10+
2+3,5
11+
3+3,6
12+
2+4,6
13+
5+2,7

students/abdul/quiz.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package quiz
2+
3+
import (
4+
"bufio"
5+
"encoding/csv"
6+
"flag"
7+
"fmt"
8+
"io"
9+
"log"
10+
"os"
11+
"strings"
12+
"time"
13+
)
14+
15+
//Number of Questions to ask
16+
const totalQuestions = 5
17+
18+
//Question struct that stores question with answer
19+
type Question struct {
20+
question string
21+
answer string
22+
}
23+
24+
func main() {
25+
filename, timeLimit := readArguments()
26+
f, err := openFile(filename)
27+
if err != nil {
28+
return
29+
}
30+
questions, err := readCSV(f)
31+
32+
if err != nil {
33+
// err := fmt.Errorf("Error in Reading Questions")
34+
fmt.Println(err.Error())
35+
return
36+
}
37+
38+
if questions == nil {
39+
return
40+
}
41+
score, err := askQuestion(questions, timeLimit)
42+
if err != nil {
43+
fmt.Println(err)
44+
return
45+
}
46+
47+
fmt.Printf("Your Score %d/%d\n", score, totalQuestions)
48+
}
49+
50+
func readArguments() (string, int) {
51+
filename := flag.String("filename", "problem.csv", "CSV File that conatins quiz questions")
52+
timeLimit := flag.Int("limit", 30, "Time Limit for each question")
53+
flag.Parse()
54+
return *filename, *timeLimit
55+
}
56+
57+
func readCSV(f io.Reader) ([]Question, error) {
58+
// defer f.Close() // this needs to be after the err check
59+
allQuestions, err := csv.NewReader(f).ReadAll()
60+
if err != nil {
61+
return nil, err
62+
}
63+
64+
numOfQues := len(allQuestions)
65+
if numOfQues == 0 {
66+
return nil, fmt.Errorf("No Question in file")
67+
}
68+
69+
var data []Question
70+
for _, line := range allQuestions {
71+
ques := Question{}
72+
ques.question = line[0]
73+
ques.answer = line[1]
74+
data = append(data, ques)
75+
}
76+
77+
return data, nil
78+
}
79+
80+
func openFile(filename string) (io.Reader, error) {
81+
return os.Open(filename)
82+
}
83+
func getInput(input chan string) {
84+
for {
85+
in := bufio.NewReader(os.Stdin)
86+
result, err := in.ReadString('\n')
87+
if err != nil {
88+
log.Fatal(err)
89+
}
90+
91+
input <- result
92+
}
93+
}
94+
95+
func askQuestion(questions []Question, timeLimit int) (int, error) {
96+
totalScore := 0
97+
timer := time.NewTimer(time.Duration(timeLimit) * time.Second)
98+
done := make(chan string)
99+
100+
go getInput(done)
101+
102+
for i := range [totalQuestions]int{} {
103+
ans, err := eachQuestion(questions[i].question, questions[i].answer, timer.C, done)
104+
if err != nil && ans == -1 {
105+
return totalScore, nil
106+
}
107+
totalScore += ans
108+
109+
}
110+
return totalScore, nil
111+
}
112+
113+
func eachQuestion(Quest string, answer string, timer <-chan time.Time, done <-chan string) (int, error) {
114+
fmt.Printf("%s: ", Quest)
115+
116+
for {
117+
select {
118+
case <-timer:
119+
return -1, fmt.Errorf("Time out")
120+
case ans := <-done:
121+
score := 0
122+
if strings.Compare(strings.Trim(strings.ToLower(ans), "\n"), answer) == 0 {
123+
score = 1
124+
} else {
125+
return 0, fmt.Errorf("Wrong Answer")
126+
}
127+
128+
return score, nil
129+
}
130+
}
131+
}

students/abdul/quiz_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package quiz
2+
3+
import (
4+
"strings"
5+
"testing"
6+
"time"
7+
8+
"gotest.tools/assert"
9+
)
10+
11+
func testEachQuestion(t *testing.T) {
12+
timer := time.NewTimer(time.Duration(2) * time.Second).C
13+
done := make(chan string)
14+
var quest Question
15+
quest.question = "1+1"
16+
quest.answer = "2"
17+
var ans int
18+
var err error
19+
allDone := make(chan bool)
20+
go func() {
21+
ans, err = eachQuestion(quest.question, quest.answer, timer, done)
22+
allDone <- true
23+
}()
24+
done <- "2"
25+
26+
<-allDone
27+
if err != nil {
28+
t.Error(err)
29+
}
30+
assert.Equal(t, ans, 1)
31+
}
32+
33+
func testReadCSV(t *testing.T) {
34+
str := "1+1,2\n2+1,3\n9+9,18\n"
35+
quest, err := readCSV(strings.NewReader(str))
36+
if err != nil {
37+
t.Error(err)
38+
}
39+
var que [3]Question
40+
que[0].answer = "2"
41+
que[1].answer = "3"
42+
que[2].answer = "18"
43+
que[0].question = "1+1"
44+
que[1].question = "2+1"
45+
que[2].question = "9+9"
46+
47+
assert.Equal(t, que[0], quest[0])
48+
assert.Equal(t, que[1], quest[1])
49+
assert.Equal(t, que[2], quest[2])
50+
51+
}
52+
53+
func TestEachQuestion(t *testing.T) {
54+
t.Run("test eachQuestion", testEachQuestion)
55+
}
56+
57+
func TestReadCSV(t *testing.T) {
58+
t.Run("test ReadCSV", testReadCSV)
59+
}

0 commit comments

Comments
 (0)