Ejemplo n.º 1
0
func DirectedGraphArcsFilterSpec(c gospec.Context) {
	gr := NewDirectedMap()
	gr.AddArc(1, 2)
	gr.AddArc(2, 3)
	gr.AddArc(3, 4)
	gr.AddArc(2, 4)
	gr.AddArc(4, 5)
	gr.AddArc(1, 6)
	gr.AddArc(2, 6)

	c.Specify("Single filtered arc", func() {
		ftail := VertexId(2)
		fhead := VertexId(3)
		f := NewDirectedGraphArcFilter(gr, ftail, fhead)

		c.Specify("shouldn't be checked", func() {
			c.Expect(f.CheckArc(ftail, fhead), IsFalse)
		})

		c.Specify("shouldn't appear in accessors", func() {
			c.Expect(CollectVertexes(f.GetAccessors(VertexId(ftail))), Not(Contains), fhead)
		})
		c.Specify("shouldn't appear in predecessors", func() {
			c.Expect(CollectVertexes(f.GetPredecessors(VertexId(fhead))), Not(Contains), ftail)
		})
		c.Specify("shouldn't appear in iterator", func() {
			for conn := range f.ArcsIter() {
				c.Expect(conn.Tail == ftail && conn.Head == fhead, IsFalse)
			}
		})
	})
}
Ejemplo n.º 2
0
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
				}
			}
		}
	})
}
Ejemplo n.º 3
0
func ActionSpec(c gospec.Context) {
	game.RegisterActions()
	c.Specify("Actions are loaded properly.", func() {
		basic := game.MakeAction("Basic Test")
		_, ok := basic.(*actions.BasicAttack)
		c.Expect(ok, Equals, true)
	})

	c.Specify("Actions can be gobbed without loss of type.", func() {
		buf := bytes.NewBuffer(nil)
		enc := gob.NewEncoder(buf)

		var as []game.Action
		as = append(as, game.MakeAction("Move Test"))
		as = append(as, game.MakeAction("Basic Test"))

		err := enc.Encode(as)
		c.Assume(err, Equals, nil)

		dec := gob.NewDecoder(buf)
		var as2 []game.Action
		err = dec.Decode(&as2)
		c.Assume(err, Equals, nil)

		_, ok := as2[0].(*actions.Move)
		c.Expect(ok, Equals, true)

		_, ok = as2[1].(*actions.BasicAttack)
		c.Expect(ok, Equals, true)

	})
}
Ejemplo n.º 4
0
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)
	})
}
Ejemplo n.º 5
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)
		}
	})
}
Ejemplo n.º 6
0
func FFTR2CSpec(c gospec.Context) {
	signal := make([]float64, 16)
	F_signal := make([]complex128, 9)

	for i := range signal {
		signal[i] = math.Sin(float64(i) / float64(len(signal)) * math.Pi * 2)
	}
	forward := PlanDftR2C1d(signal, F_signal, Estimate)
	forward.Execute()
	c.Specify("Running a R2C transform doesn't destroy the input.", func() {
		for i := range signal {
			c.Expect(signal[i], gospec.Equals, math.Sin(float64(i)/float64(len(signal))*math.Pi*2))
		}
	})

	c.Specify("Forward 1d Real to Complex FFT  works properly.", func() {
		c.Expect(real(F_signal[0]), gospec.IsWithin(1e-9), 0.0)
		c.Expect(imag(F_signal[0]), gospec.IsWithin(1e-9), 0.0)
		c.Expect(real(F_signal[1]), gospec.IsWithin(1e-9), 0.0)
		c.Expect(imag(F_signal[1]), gospec.IsWithin(1e-9), -float64(len(signal))/2)
		for i := 2; i < len(F_signal)-1; i++ {
			c.Expect(real(F_signal[i]), gospec.IsWithin(1e-9), 0.0)
			c.Expect(imag(F_signal[i]), gospec.IsWithin(1e-9), 0.0)
		}
	})
}
Ejemplo n.º 7
0
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)
					}
				}
			}
		}
	})
}
Ejemplo n.º 8
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)
	})
}
Ejemplo n.º 9
0
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
			}
		}
	})
}
Ejemplo n.º 10
0
func checkOrder(c gospec.Context, a adag, order []int) {
	c.Expect(len(a), Equals, len(order))
	c.Specify("Ordering contains all vertices exactly once", func() {
		all := make(map[int]bool)
		for _, v := range order {
			all[v] = true
		}
		c.Expect(len(all), Equals, len(order))
		for i := 0; i < len(a); i++ {
			c.Expect(all[i], Equals, true)
		}
	})
	c.Specify("Successors of a vertex always occur later in the ordering", func() {
		for i := 0; i < len(order); i++ {
			all := a.AllSuccessors(order[i])
			for j := range order {
				if i == j {
					continue
				}
				succ, ok := all[order[j]]
				if j < i {
					c.Expect(!ok, Equals, true)
				} else {
					c.Expect(!ok || succ, Equals, true)
				}
			}
		}
	})
}
Ejemplo n.º 11
0
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)
	})
}
Ejemplo n.º 12
0
func CheckDirectedPathSpec(c gospec.Context, checkPathFunction CheckDirectedPath) {
	gr := generateDirectedGraph1()

	c.Specify("Check path to self", func() {
		c.Expect(checkPathFunction(gr, 1, 1, nil, SimpleWeightFunc), IsTrue)
		c.Expect(checkPathFunction(gr, 6, 6, nil, SimpleWeightFunc), IsTrue)
	})

	c.Specify("Check neighbours path", func() {
		c.Expect(checkPathFunction(gr, 1, 2, nil, SimpleWeightFunc), IsTrue)
		c.Expect(checkPathFunction(gr, 2, 4, nil, SimpleWeightFunc), IsTrue)
		c.Expect(checkPathFunction(gr, 1, 6, nil, SimpleWeightFunc), IsTrue)
	})

	c.Specify("Check reversed neighbours", func() {
		c.Expect(checkPathFunction(gr, 6, 1, nil, SimpleWeightFunc), IsFalse)
		c.Expect(checkPathFunction(gr, 4, 3, nil, SimpleWeightFunc), IsFalse)
		c.Expect(checkPathFunction(gr, 5, 4, nil, SimpleWeightFunc), IsFalse)
	})

	c.Specify("Check long path", func() {
		c.Expect(checkPathFunction(gr, 1, 6, nil, SimpleWeightFunc), IsTrue)
		c.Expect(checkPathFunction(gr, 1, 5, nil, SimpleWeightFunc), IsTrue)
	})

	c.Specify("Check weight limit", func() {
		c.Expect(checkPathFunction(gr, 1, 5, func(node VertexId, weight float64) bool {
			return weight < 2.0
		}, SimpleWeightFunc), IsFalse)
	})
}
func MemcachedServerProcessSpecs(c gospec.Context) {
	logger := log4go.NewDefaultLogger(log4go.CRITICAL)

	c.Specify("[MemcachedServerProcess] Starts a new Memcached-Server", func() {
		server, err := StartMemcachedServer(&logger)
		defer server.Close()

		c.Expect(err, gospec.Equals, nil)
		c.Expect(server, gospec.Satisfies, server != nil)
		c.Expect(server.logger, gospec.Equals, &logger)
		c.Expect(server.port, gospec.Satisfies, server.port >= 1024)
		c.Expect(server.cmd, gospec.Satisfies, nil != server.cmd)
		c.Expect(server.connection, gospec.Satisfies, nil == server.connection)
	})

	c.Specify("[MemcachedServerProcess] Creates a connection to a Memcached-Server", func() {
		server, err := StartMemcachedServer(&logger)
		defer server.Close()

		c.Expect(err, gospec.Equals, nil)
		c.Expect(server, gospec.Satisfies, server != nil)

		connection := server.Connection()
		c.Expect(connection, gospec.Satisfies, nil != connection)
		c.Expect(server.connection, gospec.Equals, connection)

		err = connection.Open()
		c.Expect(err, gospec.Equals, nil)
		c.Expect(connection.IsOpen(), gospec.Equals, true)
	})
}
Ejemplo n.º 14
0
func FFT1dSpec(c gospec.Context) {
	signal := Alloc1d(16)
	for i := range signal {
		signal[i] = complex(float64(i), float64(-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(float64(i), float64(-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(math.Cos(float64(i)/float64(len(signal))*math.Pi*2), 0)
	}
	forward.Execute()
	c.Specify("Forward 1d FFT works properly.", func() {
		c.Expect(real(signal[0]), gospec.IsWithin(1e-9), 0.0)
		c.Expect(imag(signal[0]), gospec.IsWithin(1e-9), 0.0)
		c.Expect(real(signal[1]), gospec.IsWithin(1e-9), float64(len(signal))/2)
		c.Expect(imag(signal[1]), gospec.IsWithin(1e-9), 0.0)
		for i := 2; i < len(signal)-1; i++ {
			c.Expect(real(signal[i]), gospec.IsWithin(1e-9), 0.0)
			c.Expect(imag(signal[i]), gospec.IsWithin(1e-9), 0.0)
		}
		c.Expect(real(signal[len(signal)-1]), gospec.IsWithin(1e-9), float64(len(signal))/2)
		c.Expect(imag(signal[len(signal)-1]), gospec.IsWithin(1e-9), 0.0)
	})
}
Ejemplo n.º 15
0
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
				}
			}
		}
	})
}
Ejemplo n.º 16
0
func ChooserSpec(c gospec.Context) {
	c.Specify("Choose on []int", func() {
		a := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
		var b []int
		b = algorithm.Choose(a, func(v interface{}) bool { return v.(int)%2 == 0 }).([]int)
		c.Expect(b, ContainsInOrder, []int{0, 2, 4, 6, 8})

		b = algorithm.Choose(a, func(v interface{}) bool { return v.(int)%2 == 1 }).([]int)
		c.Expect(b, ContainsInOrder, []int{1, 3, 5, 7, 9})

		b = algorithm.Choose(a, func(v interface{}) bool { return true }).([]int)
		c.Expect(b, ContainsInOrder, a)

		b = algorithm.Choose(a, func(v interface{}) bool { return false }).([]int)
		c.Expect(b, ContainsInOrder, []int{})

		b = algorithm.Choose([]int{}, func(v interface{}) bool { return false }).([]int)
		c.Expect(b, ContainsInOrder, []int{})
	})

	c.Specify("Choose on []string", func() {
		a := []string{"foo", "bar", "wing", "ding", "monkey", "machine"}
		var b []string
		b = algorithm.Choose(a, func(v interface{}) bool { return v.(string) > "foo" }).([]string)
		c.Expect(b, ContainsInOrder, []string{"wing", "monkey", "machine"})

		b = algorithm.Choose(a, func(v interface{}) bool { return v.(string) < "foo" }).([]string)
		c.Expect(b, ContainsInOrder, []string{"bar", "ding"})
	})
}
Ejemplo n.º 17
0
func InterfaceTypeRegistrySpec(c gospec.Context) {
	c.Specify("Test that types containing interfaces are properly encoded.", func() {
		var tr core.TypeRegistry
		tr.Register(encodable6{})
		tr.Register(BarerImpl1{})
		tr.Register(BarerImpl2{})
		tr.Complete()

		v1 := encodable6{
			Interface: BarerImpl1{10, "foo"},
		}
		v2 := encodable6{
			Interface: BarerImpl2{3, 4},
		}

		b := bytes.NewBuffer(nil)
		err := tr.Encode(v1, b)
		c.Assume(err, gospec.Equals, error(nil))
		err = tr.Encode(v2, b)
		c.Assume(err, gospec.Equals, error(nil))

		d1, err := tr.Decode(b)
		c.Assume(err, gospec.Equals, error(nil))
		d2, err := tr.Decode(b)
		c.Assume(err, gospec.Equals, error(nil))

		c1, ok := d1.(encodable6)
		c.Assume(ok, gospec.Equals, true)
		c.Expect(c1.Interface.Bar(), gospec.Equals, "10:foo")
		c2, ok := d2.(encodable6)
		c.Assume(ok, gospec.Equals, true)
		c.Expect(c2.Interface.Bar(), gospec.Equals, "3:4")
	})
}
Ejemplo n.º 18
0
func AxisSpec(c gospec.Context) {
	input := gin.Make()

	// TODO: This is the mouse x axis key, we need a constant for this or something
	x := input.GetKey(300)
	events := make([]gin.OsEvent, 0)

	c.Specify("Axes aggregate press amts and report IsDown() properly.", func() {
		injectEvent(&events, x.Id(), 1, 5)
		injectEvent(&events, x.Id(), 10, 6)
		injectEvent(&events, x.Id(), -3, 7)
		input.Think(10, false, events)
		c.Expect(x.FramePressAmt(), Equals, -3.0)
		c.Expect(x.FramePressSum(), Equals, 8.0)
	})

	c.Specify("Axes can sum to zero and still be down.", func() {
		input.Think(0, false, events)
		events = events[0:0]
		c.Expect(x.FramePressSum(), Equals, 0.0)
		c.Expect(x.IsDown(), Equals, false)

		injectEvent(&events, x.Id(), 5, 5)
		injectEvent(&events, x.Id(), -5, 6)
		input.Think(10, false, events)
		events = events[0:0]
		c.Expect(x.FramePressSum(), Equals, 0.0)
		c.Expect(x.IsDown(), Equals, true)

		input.Think(20, false, events)
		c.Expect(x.FramePressSum(), Equals, 0.0)
		c.Expect(x.IsDown(), Equals, false)
	})
}
Ejemplo n.º 19
0
func FindPortSpecs(c gospec.Context) {
	c.Specify("[findPort] Finds an open port", func() {
		port, err := findPort()
		c.Expect(err, gospec.Equals, nil)
		c.Expect(port, gospec.Satisfies, port >= 1024)
	})

}
Ejemplo n.º 20
0
// Helpers
func DecodePositionSpecs(c gospec.Context) {
	c.Specify("[Decode] Decodes GeoHash to PreciseLocation + Error", func() {
		value := Decode("ww8p1r4t8")

		c.Expect(value.Location.Latitude, gospec.Satisfies, value.Location.Latitude >= 37.83 && value.Location.Latitude <= (37.84))
		c.Expect(value.Location.Longitude, gospec.Satisfies, value.Location.Longitude >= 112.55 && value.Location.Longitude <= (112.56))
	})
}
Ejemplo n.º 21
0
func PolyOverlapSpec(c gospec.Context) {
	c.Specify("Check that ConvexPolysOverlap() works properly.", func() {
		// p0 contains p1, p2 and p3
		// p1 is disjoint from everything else
		// p2 and p3 share a segment
		// p3 and p4 share a vertex
		// p4 and p5 intersect
		p0 := linear.Poly{
			linear.Vec2{0, 0},
			linear.Vec2{0, 10},
			linear.Vec2{10, 10},
			linear.Vec2{10, 0},
		}
		p1 := linear.Poly{
			linear.Vec2{1, 7},
			linear.Vec2{1, 9},
			linear.Vec2{3, 9},
			linear.Vec2{3, 7},
		}
		p2 := linear.Poly{
			linear.Vec2{1, 3},
			linear.Vec2{1, 5},
			linear.Vec2{3, 5},
			linear.Vec2{3, 3},
		}
		p3 := linear.Poly{
			linear.Vec2{3, 3},
			linear.Vec2{3, 5},
			linear.Vec2{5, 5},
			linear.Vec2{5, 3},
		}
		p4 := linear.Poly{
			linear.Vec2{5, 5},
			linear.Vec2{5, 6},
			linear.Vec2{6, 6},
		}
		p5 := linear.Poly{
			linear.Vec2{5, 1},
			linear.Vec2{6, 10},
			linear.Vec2{7, 2},
		}
		c.Expect(linear.ConvexPolysOverlap(p0, p1), Equals, true)
		c.Expect(linear.ConvexPolysOverlap(p0, p2), Equals, true)
		c.Expect(linear.ConvexPolysOverlap(p0, p3), Equals, true)
		c.Expect(linear.ConvexPolysOverlap(p0, p4), Equals, true)
		c.Expect(linear.ConvexPolysOverlap(p0, p5), Equals, true)
		c.Expect(linear.ConvexPolysOverlap(p1, p2), Equals, false)
		c.Expect(linear.ConvexPolysOverlap(p1, p3), Equals, false)
		c.Expect(linear.ConvexPolysOverlap(p1, p4), Equals, false)
		c.Expect(linear.ConvexPolysOverlap(p1, p5), Equals, false)
		c.Expect(linear.ConvexPolysOverlap(p2, p3), Equals, false)
		c.Expect(linear.ConvexPolysOverlap(p2, p4), Equals, false)
		c.Expect(linear.ConvexPolysOverlap(p2, p5), Equals, false)
		c.Expect(linear.ConvexPolysOverlap(p3, p4), Equals, false)
		c.Expect(linear.ConvexPolysOverlap(p3, p5), Equals, false)
		c.Expect(linear.ConvexPolysOverlap(p4, p5), Equals, true)
	})
}
Ejemplo n.º 22
0
func AcceptanceSpec(c gospec.Context) {

	c.Specify("When send structed data should return stracted data", func() {

		expectedData := &ResponseData{0, "3, 5, 7, 20"}
		actualData := &TaskData{0, PING, "3, 5, 7, 20"}

		var result = SendRequest(actualData)
		c.Expect(result.TaskId, Equals, expectedData.TaskId)
		c.Expect(result.ResultData, Equals, expectedData.ResultData)
	})

	/*c.Specify("When send unsorted strings data should return sorted strings data", func() {

				actualData := &TaskData{0, SORT, "7, 3, 20, 5"}
				expectedData := &TaskData{0, SORT, "3, 5, 7, 20"}

				var result = SendRequest(actualData)
				c.Expect(result.TaskData, Equals, expectedData.TaskData)
		})

		c.Specify("When send unsorted strings data processor should return sorted strings data", func() {
				actualData := &TaskData{0, SORT, "7, 3, 40, 5"}
				expectedData := &ResponseData{0, "3, 5, 7, 40"}

				requestChannel := make(chan domain.TaskData)
				responseChannel := make(chan domain.ResponseData)
				go server.TaskProcessor(requestChannel, responseChannel)
				requestChannel <-  *actualData

				var result = <- responseChannel
				c.Expect(result.ResultData, Equals, expectedData.ResultData)
				c.Expect(result.TaskId, Equals, expectedData.TaskId)

		})

		c.Specify("Test task processor with many tasks", func() {
				actualData := &TaskData{0, SORT, "7, 3, 40, 5"}
				expectedData := &ResponseData{0, "3, 5, 7, 40"}

				requestChannel := make(chan domain.TaskData, 100)
				responseChannel := make(chan domain.ResponseData, 100)
				go server.TaskProcessor(requestChannel, responseChannel)

				for i := 0; i < 10; i++ {
					requestChannel <-  *actualData
				}
				killPackage := &TaskData{0, KILL, "7, 3, 40, 5"}
				requestChannel <-  *killPackage

				for i := 0; i < 10; i++ {
	//				requestChannel <-  *actualData
					var result = <- responseChannel
					c.Expect(result.ResultData, Equals, expectedData.ResultData)
					c.Expect(result.TaskId, Equals, expectedData.TaskId)
				}
			})*/
}
Ejemplo n.º 23
0
func FFT3dSpec(c gospec.Context) {
	signal := Alloc3d(32, 16, 8)
	for i := range signal {
		for j := range signal[i] {
			for k := range signal[i][j] {
				signal[i][j][k] = complex(float64(i+j+k), float64(-i-j-k))
			}
		}
	}
	forward := PlanDft3d(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 {
			for j := range signal[i] {
				for k := range signal[i][j] {
					c.Expect(signal[i][j][k], gospec.Equals, complex(float64(i+j+k), float64(-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 := len(signal)
	fx := float64(dx) / 4
	dy := len(signal[0])
	fy := float64(dy) / 4
	dz := len(signal[0][0])
	fz := float64(dz) / 4
	for i := range signal {
		for j := range signal[i] {
			for k := range signal[i][j] {
				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[i][j][k] = complex(cosx*cosy*cosz, 0)
			}
		}
	}
	forward.Execute()
	c.Specify("Forward 3d FFT works properly.", func() {
		for i := range signal {
			for j := range signal[i] {
				for k := range signal[i][j] {
					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[i][j][k]), gospec.IsWithin(1e-7), float64(dx*dy*dz/8))
						c.Expect(imag(signal[i][j][k]), gospec.IsWithin(1e-7), 0.0)
					} else {
						c.Expect(real(signal[i][j][k]), gospec.IsWithin(1e-7), 0.0)
						c.Expect(imag(signal[i][j][k]), gospec.IsWithin(1e-7), 0.0)
					}
				}
			}
		}
	})
}
Ejemplo n.º 24
0
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")
	})
}
Ejemplo n.º 25
0
func BlockWriteTypeRegistrySpec(c gospec.Context) {
	c.Specify("Test that the coder writes complete values all at once.", func() {
		var tr core.TypeRegistry
		tr.Register(encodable1{})
		tr.Register(encodable2{})
		tr.Register(encodable3([]byte{}))
		tr.Register(encodable4{})
		tr.Register(encodable5{})
		tr.Register(encodable6{})
		tr.Register(BarerImpl1{})
		tr.Complete()

		var iw = invasiveWriter{
			reg: &tr,
			buf: bytes.NewBuffer(nil),
		}

		e1 := encodable1{1, 2}
		err := tr.Encode(e1, &iw)
		c.Assume(err, gospec.Equals, error(nil))

		e2 := encodable2{14}
		err = tr.Encode(e2, &iw)
		c.Assume(err, gospec.Equals, error(nil))

		e3 := encodable3("fudgeball")
		err = tr.Encode(e3, &iw)
		c.Assume(err, gospec.Equals, error(nil))

		e4 := encodable4{
			A: e4sub1{
				B: 10,
				C: 20,
				D: e4sub2{
					E: "foo",
					F: "bar",
				},
			},
			G: 12345,
		}
		err = tr.Encode(e4, &iw)
		c.Assume(err, gospec.Equals, error(nil))

		e5 := encodable5{
			A: []byte("Monkeyball"),
		}
		err = tr.Encode(e5, &iw)
		c.Assume(err, gospec.Equals, error(nil))

		e6 := encodable6{
			Interface: BarerImpl1{10, "foo"},
		}
		err = tr.Encode(e6, &iw)
		c.Assume(err, gospec.Equals, error(nil))
	})
}
Ejemplo n.º 26
0
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)
	})
}
Ejemplo n.º 27
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)
	})
}
Ejemplo n.º 28
0
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)
	})
}
Ejemplo n.º 29
0
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)
	})
}
Ejemplo n.º 30
0
// 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")
	})
}