Skip to content

Commit

Permalink
lab02 commit
Browse files Browse the repository at this point in the history
  • Loading branch information
KeithLin724 committed Oct 12, 2023
1 parent eaf7ea6 commit 06fff49
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/lab02.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: lab02

on:
push:
paths:
- "lab02/**"

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- uses: actions/setup-go@v4
with:
go-version-file: "lab02/go.mod"
cache: false

- name: Run
working-directory: "lab02"
run: go test
11 changes: 11 additions & 0 deletions lab02/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module lab02

go 1.21.1

require github.com/stretchr/testify v1.8.4

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions lab02/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
34 changes: 34 additions & 0 deletions lab02/lab02.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import "fmt"

func main() {
var n int64

fmt.Print("Enter a number: ")
fmt.Scanln(&n)

result := Sum(n)
fmt.Println(result)
}

func Sum(n int64) string {
// TODO: Finish this function
ans := 0
str := ""
last := n

if n%7 == 0 {
last--
}

for i := 1; i < int(last); i++ {
if i%7 == 0 {
continue
}
str += fmt.Sprintf("%d+", i)
ans += i
}

return fmt.Sprintf("%s%d=%d", str, last, ans+int(last))
}
66 changes: 66 additions & 0 deletions lab02/lab02_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

import (
"fmt"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

// input and output
var testData = []struct {
int64
string
}{
{2, "1+2=3"},
{7, "1+2+3+4+5+6=21"},
{19, "1+2+3+4+5+6+8+9+10+11+12+13+15+16+17+18+19=169"},
{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"},
{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"},
{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"},
}

func TestSum(t *testing.T) {
for _, tc := range testData {
assert.Equal(t, tc.string, Sum(tc.int64))
}
}

func TestE2E(t *testing.T) {

var err error

r1, w1, _ := os.Pipe()
r2, w2, _ := os.Pipe()

stdin := os.Stdin
stdout := os.Stdout

defer func() {
os.Stdin = stdin
os.Stdout = stdout
}()

os.Stdin = r1
os.Stdout = w2

buf := make([]byte, 10240)
var n int

for _, tc := range testData {
input := fmt.Sprintf("%d\n", tc.int64)
w1.Write([]byte(input))

main()

n, err = r2.Read(buf)
if err != nil {
t.Fatal(err)
}
if n < 16 {
t.Fatal("Error")
}
assert.Equal(t, tc.string+"\n", string(buf[16:n]))
}
}

0 comments on commit 06fff49

Please sign in to comment.