-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator_test.go
98 lines (81 loc) · 6.56 KB
/
generator_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package fibonacci
import (
"math"
"strconv"
"testing"
)
func TestFibonacciNegative(t *testing.T) {
var test_values = []int{-1, -2, -3, -10, -1000, -1000000, math.MinInt64}
for _, i := range test_values {
if _, err := NewGenerator(i); err == nil {
t.Errorf("Expected NewGenerator to return error when asked for %d iterations", i)
}
}
}
func TestFibonacciNumberOfValuesGenerated(t *testing.T) {
var test_values = []int{0, 1, 2, 3, 10, 20, 100, 1000, 100000}
for _, i := range test_values {
fg, err := NewGenerator(i)
if err != nil {
t.Errorf("Expected NewGenerator to return error when asked for %d iterations", i)
}
result_chan := make(chan FibNum)
go fg.Produce(result_chan)
cnt := 0
for dont_care := range result_chan {
_ = dont_care
cnt = cnt + 1
}
if cnt != i {
t.Errorf("Generated %d numbers when asked to generate %d, maxIterations is %d", cnt, i, fg.maxIterations)
}
}
}
func TestFibonacciVerifyCorrectOutputAgainstInt(t *testing.T) {
//At 'iteration' 93 int rolls and cannot be used to validate the implemented algorithm
var test_values = []int{1, 2, 3, 10, 20, 93}
for _, i := range test_values {
fg, err := NewGenerator(i)
if err != nil {
t.Errorf("Expected NewGenerator to return successful when asked for %d iterations", i)
}
result_chan := make(chan FibNum)
go fg.Produce(result_chan)
cnt := 0
x, y := 0, 1
for val := range result_chan {
cnt = cnt + 1
if x < 0 {
t.Fatalf("Test problem on %d iteration: expected value went negative %d", cnt, x)
}
if strconv.Itoa(x) != val.String() {
t.Fatalf("Incorrect value on iteration %d of test %d: expected %d got %s",
cnt, i, x, val.String())
}
x, y = y, x+y
}
}
}
func TestFibonacciLargeValue(t *testing.T) {
iterations := 20001
fg, err := NewGenerator(iterations)
if err != nil {
t.Errorf("Expected NewGenerator to return success when asked for %d iterations", iterations)
}
result_chan := make(chan FibNum)
go fg.Produce(result_chan)
last := newFibNum(0)
for val := range result_chan {
last = val
}
// expected value is 20000th calculated from http://php.bubble.ro/fibonacci/
// that calculator starts with 1,1 instead of 0,1
expected := "2531162323732361242240155003520607291766356485802485278951929841991312781760541315230153423463758831637443488219211037689033673531462742885329724071555187618026931630449193158922771331642302030331971098689235780843478258502779200293635651897483309686042860996364443514558772156043691404155819572984971754278513112487985892718229593329483578531419148805380281624260900362993556916638613939977074685016188258584312329139526393558096840812970422952418558991855772306882442574855589237165219912238201311184749075137322987656049866305366913734924425822681338966507463855180236283582409861199212323835947891143765414913345008456022009455704210891637791911265475167769704477334859109822590053774932978465651023851447920601310106288957894301592502061560528131203072778677491443420921822590709910448617329156135355464620891788459566081572824889514296350670950824208245170667601726417091127999999941149913010424532046881958285409468463211897582215075436515584016297874572183907949257286261608612401379639484713101138120404671732190451327881433201025184027541696124114463488665359385870910331476156665889459832092710304159637019707297988417848767011085425271875588008671422491434005115288334343837778792282383576736341414410248994081564830202363820504190074504566612515965134665683289356188727549463732830075811851574961558669278847363279870595320099844676879457196432535973357128305390290471349480258751812890314779723508104229525161740643984423978659638233074463100366500571977234508464710078102581304823235436518145074482824812996511614161933313389889630935320139507075992100561077534028207257574257706278201308302642634678112591091843082665721697117838726431766741158743554298864560993255547608496686850185804659790217122426535133253371422250684486113457341827911625517128815447325958547912113242367201990672230681308819195941016156001961954700241576553750737681552256845421159386858399433450045903975167084252876848848085910156941603293424067793097271128806817514906531652407763118308162377033463203514657531210413149191213595455280387631030665594589183601575340027172997222489081631144728873621805528648768511368948639522975539046995395707688938978847084621586473529546678958226255042389998718141303055036060772003887773038422366913820397748550793178167220193346017430024134496141145991896227741842515718997898627269918236920453493946658273870473264523119133765447653295022886429174942653014656521909469613184983671431465934965489425515981067546087342348350724207583544436107294087637975025147846254526938442435644928231027868701394819091132912397475713787593612758364812687556725146456646878912169274219209708166678668152184941578590201953144030519381922273252666652671717526318606676754556170379350956342095455612780202199922615392785572481747913435560866995432578680971243966868110016581395696310922519803685837460795358384618017215468122880442252343684547233668502313239328352671318130604247460452134121833305284398726438573787798499612760939462427922917659263046333084007208056631996856315539698234022953452211505675629153637867252695056925345220084020071611220575700841268302638995272842160994219632684575364180160991884885091858259996299627148614456696661412745040519981575543804847463997422326563897043803732970397488471644906183310144691243649149542394691524972023935190633672827306116525712882959108434211652465621144702015336657459532134026915214509960877430595844287585350290234547564574848753110281101545931547225811763441710217452979668178025286460158324658852904105792472468108996135476637212057508192176910900422826969523438985332067597093454021924077101784215936539638808624420121459718286059401823614213214326004270471752802725625810953787713898846144256909835116371235019527013180204030167601567064268573820697948868982630904164685161783088076506964317303709708574052747204405282785965604677674192569851918643651835755242670293612851920696732320545562286110332140065912751551110134916256237884844001366366654055079721985816714803952429301558096968202261698837096090377863017797020488044826628817462866854321356787305635653577619877987998113667928954840972022833505708587561902023411398915823487627297968947621416912816367516125096563705174220460639857683971213093125"
if len(last.String()) != len(expected) {
t.Errorf("%dth value was not the correct length got:%d expected:%d",
iterations, len(last.String()), len(expected))
}
if last.String() != expected {
t.Errorf("%dth value did not match the expected\ngot:%s#\nexpected:%s#\n", iterations, last, expected)
}
}