Skip to content

Commit 3ab3cfc

Browse files
committed
feat: add lab2
1 parent c00e19c commit 3ab3cfc

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

.github/workflows/lab2.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: lab2
5+
6+
on:
7+
push:
8+
paths:
9+
- 'lab2/**'
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: 'lab2/go.mod'
22+
cache: false
23+
24+
- name: Run
25+
working-directory: 'lab2'
26+
run: go test

lab2/lab2.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var n int64
7+
8+
fmt.Print("Enter a number: ")
9+
fmt.Scanln(&n)
10+
11+
result := Sum(n)
12+
fmt.Println(result)
13+
}
14+
15+
func Sum(n int64) string {
16+
// TODO: Finish this function
17+
}

lab2/lab2_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
// input and output
12+
var testData = []struct {
13+
int64
14+
string
15+
}{
16+
{2, "1+2=3"},
17+
{7, "1+2+3+4+5+6=21"},
18+
{19, "1+2+3+4+5+6+8+9+10+11+12+13+15+16+17+18+19=169"},
19+
{37, "1+2+3+4+5+6+8+9+10+11+12+13+15+16+17+18+19+20+22+23+24+25+26+27+29+30+31+32+33+34+36+37=598"},
20+
{439, "1+2+3+4+5+6+8+9+10+11+12+13+15+16+17+18+19+20+22+23+24+25+26+27+29+30+31+32+33+34+36+37+38+39+40+41+43+44+45+46+47+48+50+51+52+53+54+55+57+58+59+60+61+62+64+65+66+67+68+69+71+72+73+74+75+76+78+79+80+81+82+83+85+86+87+88+89+90+92+93+94+95+96+97+99+100+101+102+103+104+106+107+108+109+110+111+113+114+115+116+117+118+120+121+122+123+124+125+127+128+129+130+131+132+134+135+136+137+138+139+141+142+143+144+145+146+148+149+150+151+152+153+155+156+157+158+159+160+162+163+164+165+166+167+169+170+171+172+173+174+176+177+178+179+180+181+183+184+185+186+187+188+190+191+192+193+194+195+197+198+199+200+201+202+204+205+206+207+208+209+211+212+213+214+215+216+218+219+220+221+222+223+225+226+227+228+229+230+232+233+234+235+236+237+239+240+241+242+243+244+246+247+248+249+250+251+253+254+255+256+257+258+260+261+262+263+264+265+267+268+269+270+271+272+274+275+276+277+278+279+281+282+283+284+285+286+288+289+290+291+292+293+295+296+297+298+299+300+302+303+304+305+306+307+309+310+311+312+313+314+316+317+318+319+320+321+323+324+325+326+327+328+330+331+332+333+334+335+337+338+339+340+341+342+344+345+346+347+348+349+351+352+353+354+355+356+358+359+360+361+362+363+365+366+367+368+369+370+372+373+374+375+376+377+379+380+381+382+383+384+386+387+388+389+390+391+393+394+395+396+397+398+400+401+402+403+404+405+407+408+409+410+411+412+414+415+416+417+418+419+421+422+423+424+425+426+428+429+430+431+432+433+435+436+437+438+439=82909"},
21+
{1021, "1+2+3+4+5+6+8+9+10+11+12+13+15+16+17+18+19+20+22+23+24+25+26+27+29+30+31+32+33+34+36+37+38+39+40+41+43+44+45+46+47+48+50+51+52+53+54+55+57+58+59+60+61+62+64+65+66+67+68+69+71+72+73+74+75+76+78+79+80+81+82+83+85+86+87+88+89+90+92+93+94+95+96+97+99+100+101+102+103+104+106+107+108+109+110+111+113+114+115+116+117+118+120+121+122+123+124+125+127+128+129+130+131+132+134+135+136+137+138+139+141+142+143+144+145+146+148+149+150+151+152+153+155+156+157+158+159+160+162+163+164+165+166+167+169+170+171+172+173+174+176+177+178+179+180+181+183+184+185+186+187+188+190+191+192+193+194+195+197+198+199+200+201+202+204+205+206+207+208+209+211+212+213+214+215+216+218+219+220+221+222+223+225+226+227+228+229+230+232+233+234+235+236+237+239+240+241+242+243+244+246+247+248+249+250+251+253+254+255+256+257+258+260+261+262+263+264+265+267+268+269+270+271+272+274+275+276+277+278+279+281+282+283+284+285+286+288+289+290+291+292+293+295+296+297+298+299+300+302+303+304+305+306+307+309+310+311+312+313+314+316+317+318+319+320+321+323+324+325+326+327+328+330+331+332+333+334+335+337+338+339+340+341+342+344+345+346+347+348+349+351+352+353+354+355+356+358+359+360+361+362+363+365+366+367+368+369+370+372+373+374+375+376+377+379+380+381+382+383+384+386+387+388+389+390+391+393+394+395+396+397+398+400+401+402+403+404+405+407+408+409+410+411+412+414+415+416+417+418+419+421+422+423+424+425+426+428+429+430+431+432+433+435+436+437+438+439+440+442+443+444+445+446+447+449+450+451+452+453+454+456+457+458+459+460+461+463+464+465+466+467+468+470+471+472+473+474+475+477+478+479+480+481+482+484+485+486+487+488+489+491+492+493+494+495+496+498+499+500+501+502+503+505+506+507+508+509+510+512+513+514+515+516+517+519+520+521+522+523+524+526+527+528+529+530+531+533+534+535+536+537+538+540+541+542+543+544+545+547+548+549+550+551+552+554+555+556+557+558+559+561+562+563+564+565+566+568+569+570+571+572+573+575+576+577+578+579+580+582+583+584+585+586+587+589+590+591+592+593+594+596+597+598+599+600+601+603+604+605+606+607+608+610+611+612+613+614+615+617+618+619+620+621+622+624+625+626+627+628+629+631+632+633+634+635+636+638+639+640+641+642+643+645+646+647+648+649+650+652+653+654+655+656+657+659+660+661+662+663+664+666+667+668+669+670+671+673+674+675+676+677+678+680+681+682+683+684+685+687+688+689+690+691+692+694+695+696+697+698+699+701+702+703+704+705+706+708+709+710+711+712+713+715+716+717+718+719+720+722+723+724+725+726+727+729+730+731+732+733+734+736+737+738+739+740+741+743+744+745+746+747+748+750+751+752+753+754+755+757+758+759+760+761+762+764+765+766+767+768+769+771+772+773+774+775+776+778+779+780+781+782+783+785+786+787+788+789+790+792+793+794+795+796+797+799+800+801+802+803+804+806+807+808+809+810+811+813+814+815+816+817+818+820+821+822+823+824+825+827+828+829+830+831+832+834+835+836+837+838+839+841+842+843+844+845+846+848+849+850+851+852+853+855+856+857+858+859+860+862+863+864+865+866+867+869+870+871+872+873+874+876+877+878+879+880+881+883+884+885+886+887+888+890+891+892+893+894+895+897+898+899+900+901+902+904+905+906+907+908+909+911+912+913+914+915+916+918+919+920+921+922+923+925+926+927+928+929+930+932+933+934+935+936+937+939+940+941+942+943+944+946+947+948+949+950+951+953+954+955+956+957+958+960+961+962+963+964+965+967+968+969+970+971+972+974+975+976+977+978+979+981+982+983+984+985+986+988+989+990+991+992+993+995+996+997+998+999+1000+1002+1003+1004+1005+1006+1007+1009+1010+1011+1012+1013+1014+1016+1017+1018+1019+1020+1021=447636"},
22+
}
23+
24+
func TestSum(t *testing.T) {
25+
for _, tc := range testData {
26+
assert.Equal(t, tc.string, Sum(tc.int64))
27+
}
28+
}
29+
30+
func TestE2E(t *testing.T) {
31+
32+
var err error
33+
34+
r1, w1, _ := os.Pipe()
35+
r2, w2, _ := os.Pipe()
36+
37+
stdin := os.Stdin
38+
stdout := os.Stdout
39+
40+
defer func() {
41+
os.Stdin = stdin
42+
os.Stdout = stdout
43+
}()
44+
45+
os.Stdin = r1
46+
os.Stdout = w2
47+
48+
buf := make([]byte, 10240)
49+
var n int
50+
51+
for _, tc := range testData {
52+
input := fmt.Sprintf("%d\n", tc.int64)
53+
w1.Write([]byte(input))
54+
55+
main()
56+
57+
n, err = r2.Read(buf)
58+
if err != nil {
59+
t.Fatal(err)
60+
}
61+
if n < 16 {
62+
t.Fatal("Error")
63+
}
64+
assert.Equal(t, tc.string + "\n" , string(buf[16:n]))
65+
}
66+
}

0 commit comments

Comments
 (0)