func ComplexOperationsSpec(c gospec.Context) { c.Specify("Vec2.DistToLine() works.", func() { centers := []linear.Vec2{ linear.Vec2{10, 12}, linear.Vec2{1, -9}, linear.Vec2{-100, -42}, linear.Vec2{0, 1232}, } radiuses := []float64{3, 10.232, 435, 1} thetas := []float64{0.001, 0.1, 1, 1.01, 0.034241, 0.789, 90, 179, 180} angles := []float64{1.01, 1.0, 1.11111, 930142} for _, center := range centers { for _, radius := range radiuses { for _, angle := range angles { for _, theta := range thetas { a := linear.Vec2{math.Cos(angle), math.Sin(angle)} b := linear.Vec2{math.Cos(angle + theta), math.Sin(angle + theta)} seg := linear.Seg2{a.Scale(radius).Add(center), b.Scale(radius).Add(center)} dist := center.DistToLine(seg) real_dist := radius * math.Cos(theta/2) if real_dist < 0 { real_dist = -real_dist } c.Expect(dist, IsWithin(1e-9), real_dist) } } } } }) }
func Alloc3dSpec(c gospec.Context) { d100x20x10 := Alloc3d(100, 20, 10) c.Specify("Allocates the appropriate memory for 3d arrays.", func() { c.Expect(len(d100x20x10), gospec.Equals, 100) for _, v := range d100x20x10 { c.Expect(len(v), gospec.Equals, 20) for _, v := range v { c.Expect(len(v), gospec.Equals, 10) } } counter := 0.0 for i := range d100x20x10 { for j := range d100x20x10[i] { for k := range d100x20x10[i][j] { d100x20x10[i][j][k] = complex(counter, 0) counter += 1.0 } } } counter = 0.0 for i := range d100x20x10 { for j := range d100x20x10[i] { for k := range d100x20x10[i][j] { c.Expect(real(d100x20x10[i][j][k]), gospec.Equals, counter) counter += 1.0 } } } }) }
// Helpers func DecodeBoundBoxSpecs(c gospec.Context) { c.Specify("[DecodeBoundBox] Decodes GeoHash to DecodedBoundBox", func() { value := DecodeBoundBox("ww8p1r4t8") c.Expect(value.Min.Latitude, gospec.Equals, 37.83236503601074) c.Expect(value.Min.Longitude, gospec.Equals, 112.55836486816406) c.Expect(value.Max.Latitude, gospec.Equals, 37.83240795135498) c.Expect(value.Max.Longitude, gospec.Equals, 112.5584077835083) }) }
func NumRemainingValuesSpec(c gospec.Context) { c.Specify("Can handle any number of terms remaining after evaluation.", func() { context := polish.MakeContext() context.AddFunc("makeZero", func() {}) context.AddFunc("makeTwo", func() (int, int) { return 1, 2 }) res, err := context.Eval("makeTwo") c.Assume(len(res), Equals, 2) c.Assume(err, Equals, nil) res, err = context.Eval("makeZero") c.Assume(len(res), Equals, 0) c.Assume(err, Equals, nil) }) }
func MatrixIndexerSpec(c gospec.Context) { size := 100 usedIds := make(map[int]bool) nodesIds := make(map[VertexId]int) for i := 0; i < size; i++ { for j := 0; j < i; j++ { connId := matrixConnectionsIndexer(VertexId(i), VertexId(j), nodesIds, size, true) _, ok := usedIds[connId] c.Expect(ok, IsFalse) usedIds[connId] = true } } }
func FFT1dSpec(c gospec.Context) { signal := Alloc1d(16) new_in := Alloc1d(16) new_out := Alloc1d(16) for i := range signal { signal[i] = complex(float32(i), float32(-i)) new_in[i] = signal[i] } forward := PlanDft1d(signal, signal, Forward, Estimate) c.Specify("Creating a plan doesn't overwrite an existing array if fftw.Estimate is used.", func() { for i := range signal { c.Expect(signal[i], gospec.Equals, complex(float32(i), float32(-i))) } }) // A simple real cosine should result in transform with two spikes, one at S[1] and one at S[-1] // The spikes should be real and have amplitude equal to len(S)/2 (because fftw doesn't normalize) for i := range signal { signal[i] = complex(float32(math.Cos(float64(i)/float64(len(signal))*math.Pi*2)), 0) new_in[i] = signal[i] } forward.Execute() c.Specify("Forward 1d FFT works properly.", func() { peakVerifier(signal, c) }) // This should also be the case when using new arrays... forward.ExecuteNewArray(new_in, new_out) c.Specify("New array Forward 1d FFT works properly", func() { peakVerifier(new_out, c) }) }
func MultiValueReturnSpec(c gospec.Context) { c.Specify("Functions with zero or more than one return values work.", func() { context := polish.MakeContext() polish.AddIntMathContext(context) rev3 := func(a, b, c int) (int, int, int) { return c, b, a } context.AddFunc("rev3", rev3) rev5 := func(a, b, c, d, e int) (int, int, int, int, int) { return e, d, c, b, a } context.AddFunc("rev5", rev5) res, err := context.Eval("- - - - rev5 rev3 1 2 rev3 4 5 6") c.Assume(len(res), Equals, 1) c.Assume(err, Equals, nil) // - - - - rev5 rev3 1 2 rev3 4 5 6 // - - - - rev5 rev3 1 2 6 5 4 // - - - - rev5 6 2 1 5 4 // - - - - 4 5 1 2 6 // - - - -1 1 2 6 // - - -2 2 6 // - -4 6 // -10 c.Expect(int(res[0].Int()), Equals, -10) }) }
func NewArray3Spec(c gospec.Context) { d100x20x10 := NewArray3(100, 20, 10) c.Specify("Allocates the appropriate memory for 3D arrays.", func() { n0, n1, n2 := d100x20x10.Dims() c.Expect(n0, gospec.Equals, 100) c.Expect(n1, gospec.Equals, 20) c.Expect(n2, gospec.Equals, 10) var counter float32 = 0.0 for i := 0; i < n0; i++ { for j := 0; j < n1; j++ { for k := 0; k < n2; k++ { d100x20x10.Set(i, j, k, complex(counter, 0)) counter += 1.0 } } } counter = 0.0 for i := 0; i < n0; i++ { for j := 0; j < n1; j++ { for k := 0; k < n2; k++ { c.Expect(real(d100x20x10.At(i, j, k)), gospec.Equals, counter) counter += 1.0 } } } }) }
func Mapper2Spec(c gospec.Context) { c.Specify("Map from []int to []float64", func() { a := []int{0, 1, 2, 3, 4} var b []float64 algorithm.Map2(a, &b, func(n int) float64 { return float64(n) }) c.Expect(b, ContainsInOrder, []float64{0, 1, 2, 3, 4}) }) // c.Specify("Map from []int to []string", func() { // a := []int{0,1,2,3,4} // var b []string // b = algorithm.Map(a, []string{}, func(v interface{}) interface{} { return fmt.Sprintf("%d", v) }).([]string) // c.Expect(b, ContainsInOrder, []string{"0", "1", "2", "3", "4"}) // }) }
func CMWCSpec(c gospec.Context) { c.Specify("CMWC32 produces the same output as SlowCMWC.", func() { fast := core.MakeCMWC32(11, 4) slow := core.MakeSlowCMWC(11, 1<<32, 1<<4) N := 100000 for i := 0; i < N; i++ { f := fast.Next() s := slow.Next() c.Expect(f, Equals, s) if f != s { break } } }) }
func QuebraLinhaTest(c gospec.Context) { c.Specify("Caso texto seja menor q tamanho maximo", func() { c.Expect(QuebraLinha("ola mundo", 14), Equals, "ola mundo") }) c.Specify("Caso texto seja maior q tamanho maximo", func() { c.Expect(QuebraLinha("lavarini lindo demais", 15), Equals, "lavarini lindo\ndemais") }) }
func ParsingSpec(c gospec.Context) { c.Specify("Whitespace is parsed properly.", func() { context := polish.MakeContext() polish.AddIntMathContext(context) res, err := context.Eval(" + 1 3") c.Assume(len(res), Equals, 1) c.Assume(err, Equals, nil) c.Expect(int(res[0].Int()), Equals, 4) }) }
func BellmanFordSingleSourceSpec(c gospec.Context) { gr := generateDirectedGraph1() marks := BellmanFordSingleSource(gr, VertexId(2), SimpleWeightFunc) c.Expect(len(marks), Equals, gr.Order()) c.Expect(PathFromMarks(marks, VertexId(6)), ContainsExactly, Values(VertexId(2), VertexId(6))) c.Expect(PathFromMarks(marks, VertexId(5)), ContainsExactly, Values(VertexId(2), VertexId(4), VertexId(5))) c.Expect(PathFromMarks(marks, VertexId(1)), ContainsExactly, Values()) }
func NewArraySpec(c gospec.Context) { d10 := NewArray(10) d100 := NewArray(100) d1000 := NewArray(1000) c.Specify("Allocates the appropriate memory for 1D arrays.", func() { c.Expect(len(d10.Elems), gospec.Equals, 10) c.Expect(len(d100.Elems), gospec.Equals, 100) c.Expect(len(d1000.Elems), gospec.Equals, 1000) }) }
func Alloc1dSpec(c gospec.Context) { d10 := Alloc1d(10) d100 := Alloc1d(100) d1000 := Alloc1d(1000) c.Specify("Allocates the appropriate memory for 1d arrays.", func() { c.Expect(len(d10), gospec.Equals, 10) c.Expect(len(d100), gospec.Equals, 100) c.Expect(len(d1000), gospec.Equals, 1000) }) }
func FFTC2RSpec(c gospec.Context) { signal := make([]float64, 16) F_signal := make([]complex128, 9) forward := PlanDftR2C1d(signal, F_signal, Estimate) backward := PlanDftC2R1d(F_signal, signal, Estimate) for i := range signal { signal[i] = float64(i) } forward.Execute() backward.Execute() c.Specify("Forward 1d Complx to Real FFT works properly.", func() { for i := 0; i < len(signal); i++ { c.Expect(signal[i], gospec.IsWithin(1e-7), float64(i*len(signal))) } }) }
func GetAllMixedPathsSpec(c gospec.Context) { gr := generateMixedGraph1() /* [1 2 4 6] [1 2 6] [1 2 3 4 6] [1 6] */ pathsCnt := 0 for path := range GetAllMixedPaths(gr, 1, 6) { pathsCnt++ c.Expect(ContainMixedPath(gr, path, true), IsTrue) } c.Expect(pathsCnt, Equals, 4) }
// Helpers func NeighborSpecs(c gospec.Context) { c.Specify("[Neighbor] North neighbor", func() { value := Neighbor("dqcjq", [2]CardialDirections{North, None}) c.Expect(string(value), gospec.Equals, "dqcjw") }) c.Specify("[Neighbor] South West neighbor", func() { value := Neighbor("dqcjq", [2]CardialDirections{South, West}) c.Expect(string(value), gospec.Equals, "dqcjj") }) }
func FFTSpec(c gospec.Context) { signal := NewArray(16) new_in := NewArray(16) for i := range signal.Elems { signal.Elems[i] = complex(float32(i), float32(-i)) new_in.Elems[i] = signal.Elems[i] } // A simple real cosine should result in transform with two spikes, one at S[1] and one at S[-1] // The spikes should be real and have amplitude equal to len(S)/2 (because fftw doesn't normalize) for i := range signal.Elems { signal.Elems[i] = complex(float32(math.Cos(float64(i)/float64(len(signal.Elems))*math.Pi*2)), 0) new_in.Elems[i] = signal.Elems[i] } NewPlan(signal, signal, Forward, Estimate).Execute().Destroy() c.Specify("Forward 1D FFT works properly.", func() { peakVerifier(signal.Elems, c) }) }
func EventListenerSpec(c gospec.Context) { input := gin.Make() AB_binding := input.MakeBinding('a', []gin.KeyId{'b'}, []bool{true}) AB := input.BindDerivedKey("AB", AB_binding) events := make([]gin.OsEvent, 0) c.Specify("Check keys report state properly while handling events", func() { injectEvent(&events, 'a', 1, 1) injectEvent(&events, 'a', 0, 2) injectEvent(&events, 'b', 1, 3) injectEvent(&events, 'a', 1, 4) injectEvent(&events, 'b', 0, 5) injectEvent(&events, 'a', 0, 6) c.Specify("Test a", func() { la := &listener{ input: input, key_id: 'a', context: c, } input.RegisterEventListener(la) la.ExpectPressCounts(1, 1, 1, 2, 2, 2) la.ExpectReleaseCounts(0, 1, 1, 1, 1, 2) la.ExpectPressAmts(1, 0, 0, 1, 1, 0) input.Think(0, false, events) }) c.Specify("Test b", func() { lb := &listener{ input: input, key_id: 'b', context: c, } input.RegisterEventListener(lb) lb.ExpectPressCounts(0, 0, 1, 1, 1, 1) lb.ExpectReleaseCounts(0, 0, 0, 0, 1, 1) lb.ExpectPressAmts(0, 0, 1, 1, 0, 0) input.Think(0, false, events) }) c.Specify("Test ab", func() { lab := &listener{ input: input, key_id: AB.Id(), context: c, } input.RegisterEventListener(lab) lab.ExpectPressCounts(0, 0, 0, 1, 1, 1) lab.ExpectReleaseCounts(0, 0, 0, 0, 0, 1) lab.ExpectPressAmts(0, 0, 0, 1, 1, 0) input.Think(0, false, events) }) }) }
func FibSpec(c gospec.Context) { fib := NewFib().Sequence(10) c.Specify("The first two Fibonacci numbers are 0 and 1", func() { c.Expect(fib[0], Equals, 0) c.Expect(fib[1], Equals, 1) }) c.Specify("Each remaining number is the sum of the previous two", func() { for i := 2; i < len(fib); i++ { c.Expect(fib[i], Equals, fib[i-1]+fib[i-2]) } }) }
func ErrorSpec(c gospec.Context) { c.Specify("Type-mismatch panics are caught and returned as errors.", func() { context := polish.MakeContext() polish.AddIntMathContext(context) _, err := context.Eval("+ 1.0 2.0") c.Assume(err.Error(), Not(Equals), nil) }) c.Specify("Panics from inside functions are caught and returned as errors.", func() { context := polish.MakeContext() context.AddFunc("panic", func() { panic("rawr") }) _, err := context.Eval("panic") c.Assume(err.Error(), Not(Equals), nil) }) }
func FFT3Spec(c gospec.Context) { signal := NewArray3(32, 16, 8) n0, n1, n2 := signal.Dims() for i := 0; i < n0; i++ { for j := 0; j < n1; j++ { for k := 0; k < n2; k++ { signal.Set(i, j, k, complex(float32(i+j+k), float32(-i-j-k))) } } } // As long as fx < dx/2, fy < dy/2, and fz < dz/2, where dx,dy,dz are the lengths in // each dimension, there will be 2^n spikes, where n is the number of dimensions. // Each spike will be real and have magnitude equal to dx*dy*dz / 2^n dx := n0 fx := float64(dx) / 4 dy := n1 fy := float64(dy) / 4 dz := n2 fz := float64(dz) / 4 for i := 0; i < n0; i++ { for j := 0; j < n1; j++ { for k := 0; k < n2; k++ { cosx := math.Cos(float64(i) / float64(dx) * fx * math.Pi * 2) cosy := math.Cos(float64(j) / float64(dy) * fy * math.Pi * 2) cosz := math.Cos(float64(k) / float64(dz) * fz * math.Pi * 2) signal.Set(i, j, k, complex(float32(cosx*cosy*cosz), 0)) } } } NewPlan3(signal, signal, Forward, Estimate).Execute().Destroy() c.Specify("Forward 3D FFT works properly.", func() { for i := 0; i < n0; i++ { for j := 0; j < n1; j++ { for k := 0; k < n2; k++ { if (i == int(fx) || i == dx-int(fx)) && (j == int(fy) || j == dy-int(fy)) && (k == int(fz) || k == dz-int(fz)) { c.Expect(real(signal.At(i, j, k)), gospec.IsWithin(1e-7), float32(dx*dy*dz/8)) c.Expect(imag(signal.At(i, j, k)), gospec.IsWithin(1e-7), 0.0) } else { c.Expect(real(signal.At(i, j, k)), gospec.IsWithin(1e-7), 0.0) c.Expect(imag(signal.At(i, j, k)), gospec.IsWithin(1e-7), 0.0) } } } } }) }
func CommandNSpec(c gospec.Context) { c.Specify("Sample sprite loads correctly", func() { s, err := sprite.LoadSprite("test_sprite") c.Expect(err, Equals, nil) for i := 0; i < 2000; i++ { s.Think(50) } s.CommandN([]string{ "turn_right", "turn_right", "turn_right", "turn_right", "turn_right", "turn_right", "turn_left", "turn_left", "turn_right", "turn_right", "turn_right", "turn_left", "turn_left"}) s.Think(5000) c.Expect(s.Facing(), Equals, 1) for i := 0; i < 3000; i++ { s.Think(50) } }) }
func SplitGraphToIndependentSubgraphs_directedSpec(c gospec.Context) { c.Specify("Directed graph with 2 independent parts", func() { gr1, gr2, gr_merged := genDgr2IndependentSubGr() subgraphs := SplitGraphToIndependentSubgraphs_directed(gr_merged) c.Expect(len(subgraphs), Equals, 2) if subgraphs[0].CheckNode(VertexId(1)) { c.Expect(DirectedGraphsEquals(subgraphs[0], gr1), IsTrue) c.Expect(DirectedGraphsEquals(subgraphs[1], gr2), IsTrue) } else { c.Expect(DirectedGraphsEquals(subgraphs[0], gr2), IsTrue) c.Expect(DirectedGraphsEquals(subgraphs[1], gr1), IsTrue) } }) }
func LoadSpriteSpec(c gospec.Context) { c.Specify("Sample sprite loads correctly", func() { s, err := sprite.LoadSprite("test_sprite") c.Expect(err, Equals, nil) for i := 0; i < 2000; i++ { s.Think(50) } s.Command("defend") s.Command("undamaged") s.Command("defend") s.Command("undamaged") for i := 0; i < 3000; i++ { s.Think(50) } s.Command("turn_right") s.Command("turn_right") s.Command("turn_right") s.Command("turn_right") s.Command("turn_right") s.Command("turn_right") s.Command("turn_left") s.Command("turn_left") s.Command("turn_right") s.Command("turn_right") s.Command("turn_right") s.Command("turn_left") s.Command("turn_left") // s.Think(5000) for i := 0; i < 300; i++ { s.Think(50) } c.Expect(s.Facing(), Equals, 1) }) }
func TriangleSpec(c gospec.Context) { c.Specify("Check that areaOfPGram() works properly.", func() { v0 := linear.Vec2{1, 0} v1 := linear.Vec2{0, 0} v2 := linear.Vec2{0, 1} c.Expect(linear.AreaOfPGram(v0, v1, v2), IsWithin(1e-9), 1.0) v3 := linear.Vec2{-1000, 1} c.Expect(linear.AreaOfPGram(v0, v1, v3), IsWithin(1e-9), 1.0) v4 := linear.Vec2{-10, 20} v5 := linear.Vec2{10, 10} v6 := linear.Vec2{-5, 10} c.Expect(linear.AreaOfPGram(v4, v5, v6), IsWithin(1e-9), 150.0) }) }
func PolySpec2(c gospec.Context) { p := linear.Poly{ {-1, 0}, {-3, 0}, {0, 10}, {3, 0}, {1, 0}, {2, 1}, {-2, 1}, } c.Specify("Check that exterior and interior segments of a polygon are correctly identified.", func() { visible_exterior := []linear.Seg2{ linear.MakeSeg2(-1, 0, -3, 0), linear.MakeSeg2(2, 1, -2, 1), linear.MakeSeg2(3, 0, 1, 0), } visible_interior := []linear.Seg2{ linear.MakeSeg2(2, 1, -2, 1), linear.MakeSeg2(-3, 0, 0, 10), linear.MakeSeg2(0, 10, 3, 0), linear.MakeSeg2(-1, 0, -3, 0), linear.MakeSeg2(3, 0, 1, 0), } c.Expect(p.VisibleExterior(linear.Vec2{0, -10}), ContainsExactly, visible_exterior) c.Expect(p.VisibleInterior(linear.Vec2{0, 5}), ContainsExactly, visible_interior) }) }
func CMWCRandSpec(c gospec.Context) { c.Specify("CMWC32 conforms properly to math/rand.Rand interface.", func() { c1 := cmwc.MakeCmwc(3278470471, 4) c2 := cmwc.MakeCmwc(3278470471, 4) c1.Seed(1234) c2.Seed(4321) // Make sure that we don't generate numbers with the most significant // bit set for i := 0; i < 1000000; i++ { v1 := c1.Int63() c.Expect(v1 >= 0, Equals, true) if v1 < 0 { break } c2.Int63() } // Make sure that two generators with the same parameters, but in // different states, are in the exact same state when seeded with // the same seed. c1.Seed(0xabcdef12) c2.Seed(0xabcdef12) for i := 0; i < 10000; i++ { v1 := c1.Int63() v2 := c2.Int63() c.Expect(v1, Equals, v2) if v1 != v2 { break } } }) }
func MapperSpec(c gospec.Context) { c.Specify("Map from []int to []float64", func() { a := []int{0, 1, 2, 3, 4} var b []float64 b = algorithm.Map(a, []float64{}, func(v interface{}) interface{} { return float64(v.(int)) }).([]float64) c.Expect(b, ContainsInOrder, []float64{0, 1, 2, 3, 4}) }) c.Specify("Map from []int to []string", func() { a := []int{0, 1, 2, 3, 4} var b []string b = algorithm.Map(a, []string{}, func(v interface{}) interface{} { return fmt.Sprintf("%d", v) }).([]string) c.Expect(b, ContainsInOrder, []string{"0", "1", "2", "3", "4"}) }) }