Skip to content

Commit f79d5a2

Browse files
committed
feat: add lab5
1 parent 7dd0f44 commit f79d5a2

File tree

7 files changed

+303
-0
lines changed

7 files changed

+303
-0
lines changed

.github/workflows/lab5.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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: lab5
5+
6+
on:
7+
push:
8+
paths:
9+
- 'lab5/**'
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: 'lab5/go.mod'
22+
cache: false
23+
24+
- name: Build
25+
working-directory: 'lab5'
26+
run: |
27+
pip install -r requirements.txt
28+
cp "$(go env GOROOT)/misc/wasm/wasm_exec.js" .
29+
GOOS=js GOARCH=wasm go build -o wasm/lib.wasm wasm/main.go
30+
31+
- name: Run
32+
working-directory: 'lab5'
33+
run: |
34+
go run server.go &
35+
python3 validate.py

lab5/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
**.wasm
2+
wasm_exec.js

lab5/index.html

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<html>
2+
3+
<head>
4+
<meta charset="utf-8" />
5+
<style>
6+
body {
7+
font-family: 'Arial', sans-serif;
8+
margin: 0;
9+
padding: 0;
10+
display: flex;
11+
align-items: center;
12+
justify-content: center;
13+
min-height: 100vh;
14+
background-color: #f0f0f0;
15+
}
16+
17+
.container {
18+
text-align: center;
19+
}
20+
21+
#value {
22+
padding: 15px;
23+
font-size: 16px;
24+
border: none;
25+
border-bottom: 2px solid #3f51b5;
26+
outline: none;
27+
transition: border-bottom 0.3s;
28+
margin-bottom: 20px;
29+
background-color: transparent;
30+
}
31+
32+
#value:focus {
33+
border-bottom: 2px solid #303f9f;
34+
}
35+
36+
#check {
37+
display: inline-block;
38+
padding: 10px 20px;
39+
font-size: 16px;
40+
cursor: pointer;
41+
text-align: center;
42+
border: none;
43+
border-radius: 4px;
44+
box-shadow: 0 2px 2px rgba(0, 0, 0, 0.1);
45+
transition: background-color 0.3s, box-shadow 0.3s;
46+
color: #fff;
47+
background-color: #3f51b5;
48+
}
49+
50+
#check:hover {
51+
background-color: #303f9f;
52+
box-shadow: 0 4px 4px rgba(0, 0, 0, 0.2);
53+
}
54+
55+
#check:active {
56+
background-color: #222e7a;
57+
}
58+
59+
.container>div:nth-child(2) {
60+
min-height: 1.2rem;
61+
}
62+
63+
#answer {
64+
margin-top: 10px;
65+
font-size: 18px;
66+
color: #333;
67+
}
68+
</style>
69+
<script src="wasm_exec.js"></script>
70+
<script>
71+
const go = new Go()
72+
WebAssembly.instantiateStreaming(fetch('wasm/lib.wasm'), go.importObject).then(result => {
73+
go.run(result.instance)
74+
})
75+
</script>
76+
</head>
77+
78+
<body>
79+
<div class="container">
80+
<div>
81+
<input type="number" id="value" />
82+
<button id="check" onclick="CheckPrime()">Check Prime</button>
83+
</div>
84+
<div>
85+
<span id="answer"></span>
86+
</div>
87+
</div>
88+
</body>
89+
90+
</html>

lab5/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
selenium==4.15.2

lab5/server.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/http"
7+
)
8+
9+
func main() {
10+
fmt.Println("Go server is running on http://localhost:8086")
11+
log.Fatal(http.ListenAndServe(":8086", http.FileServer(http.Dir("."))))
12+
}

lab5/validate.py

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
import os
2+
import time
3+
import urllib.request
4+
from selenium import webdriver
5+
from selenium.common.exceptions import TimeoutException
6+
from selenium.webdriver.common.by import By
7+
from selenium.webdriver.chrome.service import Service
8+
from selenium.webdriver.support import expected_conditions as EC
9+
from selenium.webdriver.support.ui import WebDriverWait
10+
11+
12+
class text_to_be_changed(object):
13+
def __init__(self, locator, original_text):
14+
self.locator = locator
15+
self.original_text = original_text
16+
17+
def __call__(self, driver):
18+
element_text = driver.find_element(*self.locator).text
19+
return element_text != self.original_text
20+
21+
22+
def main() -> int:
23+
PRIME = "It's prime"
24+
NOTPRIME = "It's not prime"
25+
26+
test_cases = [
27+
("1", NOTPRIME),
28+
("2", PRIME),
29+
("3", PRIME),
30+
("977", PRIME),
31+
("1021", PRIME),
32+
("7139", NOTPRIME),
33+
("28669", PRIME),
34+
("34860283", PRIME),
35+
("76351879", NOTPRIME),
36+
("93416783", PRIME),
37+
("672541787", PRIME),
38+
("896871567", NOTPRIME),
39+
("3472666597", PRIME),
40+
("8632541253", NOTPRIME),
41+
("9633251477", NOTPRIME),
42+
("77265818857", PRIME),
43+
("99999985837", PRIME),
44+
("99999999997", NOTPRIME),
45+
("172658105531", PRIME),
46+
("267714526633", NOTPRIME),
47+
("201423710699", PRIME),
48+
("314159265359", PRIME),
49+
("496635874121", NOTPRIME),
50+
("588736214479", NOTPRIME),
51+
("786632547711", NOTPRIME),
52+
("538941033881", PRIME),
53+
("6322571574869", NOTPRIME),
54+
("23674586397841", PRIME),
55+
("47763354178559", NOTPRIME),
56+
("99887766554433", NOTPRIME),
57+
("124475899632119", NOTPRIME),
58+
("375593357415211", NOTPRIME),
59+
("923174692024939", PRIME),
60+
("1000000000000007", NOTPRIME),
61+
("10000000000000061", PRIME),
62+
("789663254110256361", NOTPRIME),
63+
("1056325741526369751", NOTPRIME),
64+
("1000000000000000003", PRIME),
65+
("10000000000000000003", NOTPRIME),
66+
("18446744073709551615", NOTPRIME),
67+
]
68+
69+
inp = ""
70+
71+
if not os.path.exists("wasm_exec.js"):
72+
print("\033[1;31m[ERROR]\033[0m wasm_exec.js not found!")
73+
return 1
74+
if not os.path.exists("wasm/lib.wasm"):
75+
print("\033[1;31m[ERROR]\033[0m wasm/lib.wasm not found!")
76+
return 1
77+
78+
try:
79+
service = Service()
80+
options = webdriver.ChromeOptions()
81+
options.add_argument("--headless=new")
82+
driver = webdriver.Chrome(service=service, options=options)
83+
wait = WebDriverWait(driver, 3)
84+
url = "http://localhost:8086"
85+
86+
for _ in range(30):
87+
try:
88+
code = urllib.request.urlopen(url).getcode()
89+
if code == 200:
90+
break
91+
except Exception as e:
92+
print(
93+
"\033[1;36m[INFO]\033[0m Unable to connect to Go server, try again..."
94+
)
95+
time.sleep(2)
96+
else:
97+
print("\033[1;31m[ERROR]\033[0m Unable to connect to Go server!")
98+
return 1
99+
100+
driver.get(url)
101+
print("\033[1;36m[INFO]\033[0m Start testing!")
102+
103+
for tc in test_cases:
104+
input_field = driver.find_element(By.ID, "value")
105+
input_field.send_keys(tc[0])
106+
107+
check_button = driver.find_element(By.ID, "check")
108+
check_button.click()
109+
110+
inp = tc[0]
111+
112+
wait.until(text_to_be_changed((By.ID, "answer"), ""))
113+
answer = driver.find_element(By.ID, "answer")
114+
if answer.text != tc[1]:
115+
print(
116+
'\033[1;31m[ERROR]\033[0m Input: "%s", expected: "%s", got: "%s"'
117+
% (tc[0], tc[1], answer.text)
118+
)
119+
return 1
120+
driver.refresh()
121+
return 0
122+
123+
except TimeoutException as e:
124+
print('\033[1;31m[ERROR]\033[0m Time limit excceed!! Last input: "%s"' % inp)
125+
return 1
126+
127+
except Exception as e:
128+
print("\033[1;31m[ERROR]\033[0m")
129+
print(e)
130+
return 1
131+
finally:
132+
driver.quit()
133+
134+
135+
if __name__ == "__main__":
136+
status = main()
137+
if status == 0:
138+
print("\033[1;32mPASS\033[0m")
139+
exit(status)

lab5/wasm/main.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math/big"
6+
"strconv"
7+
"syscall/js"
8+
)
9+
10+
func CheckPrime(this js.Value, args []js.Value) interface{} {
11+
// TODO: Check if the number is prime
12+
}
13+
14+
func registerCallbacks() {
15+
// TODO: Register the function CheckPrime
16+
}
17+
18+
func main() {
19+
fmt.Println("Golang main function executed")
20+
registerCallbacks()
21+
22+
//need block the main thread forever
23+
select {}
24+
}

0 commit comments

Comments
 (0)