Skip to content

Commit 7dd0f44

Browse files
committed
feat: add lab4
1 parent 1968f14 commit 7dd0f44

File tree

5 files changed

+311
-0
lines changed

5 files changed

+311
-0
lines changed

.github/workflows/lab4.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# This workflow will build a golang project
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
3+
4+
name: lab4
5+
6+
on:
7+
push:
8+
paths:
9+
- 'lab4/**'
10+
11+
jobs:
12+
13+
build:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- uses: actions/checkout@v4
18+
19+
- uses: actions/setup-go@v4
20+
with:
21+
go-version-file: 'lab4/go.mod'
22+
cache: false
23+
24+
- name: Run
25+
working-directory: 'lab4'
26+
run: go test

lab4/error.html

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<!DOCTYPE html>
2+
<html lang="zh-tw">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>Error Page</title>
7+
<style>
8+
body {
9+
background-color: #f2f2f2;
10+
font-family: Arial, sans-serif;
11+
}
12+
13+
.container {
14+
margin: 0 auto;
15+
max-width: 800px;
16+
padding: 50px;
17+
text-align: center;
18+
}
19+
20+
h1 {
21+
color: #ff4d4d;
22+
font-size: 72px;
23+
margin-bottom: 20px;
24+
}
25+
26+
p {
27+
color: #666;
28+
font-size: 24px;
29+
margin-bottom: 40px;
30+
}
31+
32+
button {
33+
background-color: #ff4d4d;
34+
border: none;
35+
border-radius: 5px;
36+
color: #fff;
37+
cursor: pointer;
38+
font-size: 18px;
39+
padding: 10px 20px;
40+
transition: background-color 0.3s ease;
41+
}
42+
43+
button:hover {
44+
background-color: #e60000;
45+
}
46+
</style>
47+
</head>
48+
49+
<body>
50+
<div class="container">
51+
<h1>Error!</h1>
52+
<p>Something went wrong.</p>
53+
<button onclick="history.back()">Go back</button>
54+
</div>
55+
</body>
56+
57+
</html>

lab4/index.html

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<!DOCTYPE html>
2+
<html lang="zh-tw">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<style>
7+
body {
8+
background-color: #f2f2f2;
9+
font-family: Arial, sans-serif;
10+
}
11+
12+
.container {
13+
margin: 0 auto;
14+
max-width: 1000px;
15+
padding: 50px;
16+
text-align: center;
17+
}
18+
19+
.expression {
20+
color: #666;
21+
font-size: 72px;
22+
margin-bottom: 20px;
23+
font-weight: bold;
24+
}
25+
26+
.is {
27+
color: #666;
28+
font-size: 32px;
29+
margin-bottom: 20px;
30+
}
31+
32+
.result {
33+
color: #ff4d4d;
34+
font-size: 72px;
35+
margin-bottom: 40px;
36+
font-weight: 900;
37+
}
38+
</style>
39+
</head>
40+
41+
<body>
42+
<div class="container">
43+
<div class="expression">{{.Expression}}</div>
44+
<div class="is">is</div>
45+
<div class="result">{{.Result}}</div>
46+
</div>
47+
</body>
48+
49+
</html>

lab4/lab4.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"html/template"
6+
"log"
7+
"net/http"
8+
"strconv"
9+
)
10+
11+
// TODO: Create a struct to hold the data sent to the template
12+
13+
func Calculator(w http.ResponseWriter, r *http.Request) {
14+
// TODO: Finish this function
15+
}
16+
17+
func main() {
18+
http.HandleFunc("/", Calculator)
19+
log.Fatal(http.ListenAndServe(":8084", nil))
20+
}

lab4/lab4_test.go

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package main
2+
3+
import (
4+
"github.com/PuerkitoBio/goquery"
5+
"github.com/stretchr/testify/assert"
6+
"net/http"
7+
"net/http/httptest"
8+
"testing"
9+
)
10+
11+
func TestCalculator(t *testing.T) {
12+
13+
var tests = []struct {
14+
query string
15+
expectedWrong bool
16+
exp string
17+
res string
18+
}{
19+
{"/?op=add&num1=58270622&num2=58178886", false, "58270622 + 58178886", "116449508"},
20+
{"/?op=gcd&num1=6627265&num2=77682628", false, "GCD(6627265, 77682628)", "1"},
21+
{"/?op=gcd&num1=2520&num2=217728", false, "GCD(2520, 217728)", "504"},
22+
{"/?op=div&num1=73161081&num2=239", false, "73161081 / 239", "306113"},
23+
{"/?op=lcm&num1=2178870&num2=2264715", false, "LCM(2178870, 2264715)", "5575728330"},
24+
{"/?op=div&num1=5740658&num2=880", false, "5740658 / 880", "6523"},
25+
{"/?op=sub&num1=10357427&num2=61397368", false, "10357427 - 61397368", "-51039941"},
26+
{"/?op=add&num1=59164249&num2=99217347", false, "59164249 + 99217347", "158381596"},
27+
{"/?op=gcd&num1=4998952&num2=2164925", false, "GCD(4998952, 2164925)", "623"},
28+
{"/?op=mul&num1=29154570&num2=21713233", false, "29154570 * 21713233", "633039971424810"},
29+
{"/?op=gcd&num1=1526503&num2=5907671", false, "GCD(1526503, 5907671)", "803"},
30+
{"/?op=add&num1=72935478&num2=43105088", false, "72935478 + 43105088", "116040566"},
31+
{"/?op=div&num1=97504070&num2=721", false, "97504070 / 721", "135234"},
32+
{"/?op=gcd&num1=2398600&num2=1966584", false, "GCD(2398600, 1966584)", "536"},
33+
{"/?op=add&num1=98722180&num2=59536118", false, "98722180 + 59536118", "158258298"},
34+
{"/?op=lcm&num1=368544&num2=135828", false, "LCM(368544, 135828)", "379231776"},
35+
{"/?op=add&num1=76706194&num2=30858274", false, "76706194 + 30858274", "107564468"},
36+
{"/?op=add&num1=6764883&num2=65616199", false, "6764883 + 65616199", "72381082"},
37+
{"/?op=lcm&num1=413381&num2=843404", false, "LCM(413381, 843404)", "2220682732"},
38+
{"/?op=mul&num1=35080682&num2=42037792", false, "35080682 * 42037792", "1474714413134144"},
39+
{"/?op=lcm&num1=64014&num2=987000", false, "LCM(64014, 987000)", "224049000"},
40+
{"/?op=lcm&num1=4446409&num2=4393191", false, "LCM(4446409, 4393191)", "43312470069"},
41+
{"/?op=lcm&num1=2610312&num2=2962038", false, "LCM(2610312, 2962038)", "21125255016"},
42+
{"/?op=sub&num1=61516052&num2=20669659", false, "61516052 - 20669659", "40846393"},
43+
{"/?op=sub&num1=7597860&num2=99194032", false, "7597860 - 99194032", "-91596172"},
44+
{"/?op=lcm&num1=328245&num2=659176", false, "LCM(328245, 659176)", "2738876280"},
45+
{"/?op=sub&num1=29578428&num2=61491493", false, "29578428 - 61491493", "-31913065"},
46+
{"/?op=lcm&num1=223979&num2=78155", false, "LCM(223979, 78155)", "357246505"},
47+
{"/?op=gcd&num1=801504&num2=879648", false, "GCD(801504, 879648)", "1056"},
48+
{"/?op=sub&num1=46808093&num2=74920999", false, "46808093 - 74920999", "-28112906"},
49+
{"/?op=add&num1=8224503&num2=74546964", false, "8224503 + 74546964", "82771467"},
50+
{"/?op=gcd&num1=413646&num2=299052", false, "GCD(413646, 299052)", "426"},
51+
{"/?op=gcd&num1=35074&num2=15613", false, "GCD(35074, 15613)", "13"},
52+
{"/?op=gcd&num1=976472&num2=1927716", false, "GCD(976472, 1927716)", "1484"},
53+
{"/?op=mul&num1=68425179&num2=18965701", false, "68425179 * 18965701", "1297731485785479"},
54+
{"/?op=div&num1=17482447&num2=606", false, "17482447 / 606", "28848"},
55+
{"/?op=lcm&num1=4260438&num2=1355988", false, "LCM(4260438, 1355988)", "3331662516"},
56+
{"/?op=add&num1=99661159&num2=13844561", false, "99661159 + 13844561", "113505720"},
57+
{"/?op=gcd&num1=3403050&num2=8400672", false, "GCD(3403050, 8400672)", "19446"},
58+
{"/?op=gcd&num1=1231271&num2=909074", false, "GCD(1231271, 909074)", "509"},
59+
{"/?op=add&num1=45690037&num2=45611061", false, "45690037 + 45611061", "91301098"},
60+
{"/?op=gcd&num1=245220&num2=2272908", false, "GCD(245220, 2272908)", "804"},
61+
{"/?op=gcd&num1=6627265&num2=77682628", false, "GCD(6627265, 77682628)", "1"},
62+
{"/?op=lcm&num1=7621691&num2=594859", false, "LCM(7621691, 594859)", "4717826729"},
63+
{"/?op=gcd&num1=4125568&num2=5504988", false, "GCD(4125568, 5504988)", "668"},
64+
{"/?op=mul&num1=80536133&num2=94163376", false, "80536133 * 94163376", "7583554173265008"},
65+
{"/?op=lcm&num1=2284100&num2=1958320", false, "LCM(2284100, 1958320)", "2457691600"},
66+
{"/?op=lcm&num1=654108&num2=2760030", false, "LCM(654108, 2760030)", "3306515940"},
67+
{"/?op=gcd&num1=77616&num2=2207205", false, "GCD(77616, 2207205)", "4851"},
68+
{"/?op=div&num1=20596979&num2=724", false, "20596979 / 724", "28448"},
69+
{"/?op=sub&num1=62709732&num2=1701229", false, "62709732 - 1701229", "61008503"},
70+
{"/?op=sub&num1=5831827&num2=71624192", false, "5831827 - 71624192", "-65792365"},
71+
{"/?op=sub&num1=54951166&num2=24597974", false, "54951166 - 24597974", "30353192"},
72+
{"/?op=sub&num1=44087479&num2=4128157", false, "44087479 - 4128157", "39959322"},
73+
{"/?op=lcm&num1=620686&num2=587934", false, "LCM(620686, 587934)", "2050125858"},
74+
{"/?op=div&num1=50783652&num2=920", false, "50783652 / 920", "55199"},
75+
{"/?op=lcm&num1=3889424&num2=4441360", false, "LCM(3889424, 4441360)", "2003053360"},
76+
{"/?op=gcd&num1=1488880&num2=1463720", false, "GCD(1488880, 1463720)", "1480"},
77+
{"/?op=lcm&num1=2215026&num2=3924558", false, "LCM(2215026, 3924558)", "14634676782"},
78+
{"/?op=gcd&num1=323570&num2=2905214", false, "GCD(323570, 2905214)", "494"},
79+
{"/?op=sub&num1=9049910&num2=16822009", false, "9049910 - 16822009", "-7772099"},
80+
{"/?op=add&num1=68852091&num2=61068057", false, "68852091 + 61068057", "129920148"},
81+
{"/?op=mul&num1=68267108&num2=90345968", false, "68267108 * 90345968", "6167657954820544"},
82+
{"/?op=gcd&num1=8070&num2=221550", false, "GCD(8070, 221550)", "30"},
83+
{"/?op=lcm&num1=1278648&num2=1282088", false, "LCM(1278648, 1282088)", "4765521096"},
84+
{"/?op=lcm&num1=2750280&num2=5094855", false, "LCM(2750280, 5094855)", "21724461720"},
85+
{"/?op=lcm&num1=1225440&num2=4522806", false, "LCM(1225440, 4522806)", "8321963040"},
86+
{"/?op=gcd&num1=1521432&num2=5988774", false, "GCD(1521432, 5988774)", "7458"},
87+
{"/?op=sub&num1=16338218&num2=4672490", false, "16338218 - 4672490", "11665728"},
88+
{"/?op=add&num1=6627265&num2=77682628", false, "6627265 + 77682628", "84309893"},
89+
{"/?op=lcm&num1=4807151&num2=5083519", false, "LCM(4807151, 5083519)", "32539605119"},
90+
{"/?op=add&num1=32608227&num2=74206780", false, "32608227 + 74206780", "106815007"},
91+
{"/?op=div&num1=77995456&num2=508", false, "77995456 / 508", "153534"},
92+
{"/?op=add&num1=83632880&num2=77216163", false, "83632880 + 77216163", "160849043"},
93+
{"/?op=mul&num1=60731823&num2=21184468", false, "60731823 * 21184468", "1286571360925164"},
94+
{"/?op=div&num1=75019105&num2=51", false, "75019105 / 51", "1470962"},
95+
{"/?op=div&num1=4351043&num2=908", false, "4351043 / 908", "4791"},
96+
{"/?op=div&num1=85082565&num2=895", false, "85082565 / 895", "95064"},
97+
{"/?op=lcm&num1=866592&num2=2636064", false, "LCM(866592, 2636064)", "2643972192"},
98+
{"/?op=add&num1=89083762&num2=2507411", false, "89083762 + 2507411", "91591173"},
99+
{"/?op=lcm&num1=3998008&num2=6742624", false, "LCM(3998008, 6742624)", "15528263072"},
100+
{"/?op=sub&num1=75050538&num2=50957188", false, "75050538 - 50957188", "24093350"},
101+
{"/?op=div&num1=60387026&num2=379", false, "60387026 / 379", "159332"},
102+
{"/?op=lcm&num1=7407720&num2=5522445", false, "LCM(7407720, 5522445)", "15948821160"},
103+
{"/?op=gcd&num1=2216718&num2=2338182", false, "GCD(2216718, 2338182)", "30366"},
104+
{"/?op=gcd&num1=2353880&num2=3042448", false, "GCD(2353880, 3042448)", "664"},
105+
{"/?op=lcm&num1=111690&num2=2743290", false, "LCM(111690, 2743290)", "200260170"},
106+
{"/?op=lcm&num1=5501174&num2=4048268", false, "LCM(5501174, 4048268)", "1793382724"},
107+
{"/?op=gcd&num1=7696649&num2=9219834", false, "GCD(7696649, 9219834)", "961"},
108+
{"/?op=lcm&num1=709464&num2=5499207", false, "LCM(709464, 5499207)", "4531346568"},
109+
{"/?op=div&num1=73544141&num2=28", false, "73544141 / 28", "2626576"},
110+
{"/?op=lcm&num1=5210280&num2=6414716", false, "LCM(5210280, 6414716)", "23670302040"},
111+
{"/?op=div&num1=63967382&num2=486", false, "63967382 / 486", "131620"},
112+
{"/?op=div&num1=17375336&num2=67", false, "17375336 / 67", "259333"},
113+
{"/?op=mul&num1=98750550&num2=14664916", false, "98750550 * 14664916", "1448168520703800"},
114+
{"/?op=gcd&num1=357544&num2=65164", false, "GCD(357544, 65164)", "44"},
115+
{"/?op=lcm&num1=352352&num2=4189640", false, "LCM(352352, 4189640)", "2027785760"},
116+
{"/?op=lcm&num1=553926&num2=446512", false, "LCM(553926, 446512)", "2875983792"},
117+
{"/?op=sub&num1=11716613&num2=25785145", false, "11716613 - 25785145", "-14068532"},
118+
{"/?op=lcm&num1=8045480&num2=7934175", false, "LCM(8045480, 7934175)", "64806341400"},
119+
{"/?op=sub&num1=52997871&num2=9604950", false, "52997871 - 9604950", "43392921"},
120+
{"/?op=sub&num1=45798915&num2=17510154", false, "45798915 - 17510154", "28288761"},
121+
{"/?op=gcd", true, "", ""},
122+
{"/?op=po&num1=4547151&num2=5087519", true, "", ""},
123+
{"/?op=div&cat=meow&num2=5087519", true, "", ""},
124+
{"/?op=lcm&num1=&num2=", true, "", ""},
125+
{"/?op=div&num1=8547403num2=932541", true, "", ""},
126+
{"/?op=&num1=&num2=", true, "", ""},
127+
{"/?op=div&num1=&num2=0", true, "", ""},
128+
{"/?op=mul&num1=seven&num2=eleven", true, "", ""},
129+
{"/?op=mul&num1=8&num2=seven", true, "", ""},
130+
{"/?op=sub&num1=58270622", true, "", ""},
131+
{"/?op=gcd&a=58178886&b=123", true, "", ""},
132+
{"/?op=div&num1=58270622&num2=0", true, "", ""},
133+
{"/?skill=starbust-stream&debuff=cant-logout", true, "", ""},
134+
{"/?name=jerry&enemy=tom", true, "", ""},
135+
{"/", true, "", ""},
136+
}
137+
138+
for _, test := range tests {
139+
req, err := http.NewRequest("GET", test.query, nil)
140+
if err != nil {
141+
t.Fatal(err)
142+
}
143+
res := httptest.NewRecorder()
144+
Calculator(res, req)
145+
if res.Code != http.StatusOK {
146+
t.Errorf("expected status OK; got %v", res.Code)
147+
}
148+
doc, err := goquery.NewDocumentFromReader(res.Body)
149+
if err != nil {
150+
t.Fatal(err)
151+
}
152+
if !test.expectedWrong {
153+
assert.Equal(t, test.exp, doc.Find("body .container .expression").Text())
154+
assert.Equal(t, test.res, doc.Find("body .container .result").Text())
155+
} else {
156+
assert.Equal(t, "Error!", doc.Find("body .container h1").Text())
157+
}
158+
}
159+
}

0 commit comments

Comments
 (0)