tamias_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package tamias
  2. import "testing"
  3. func assert(test * testing.T, cond bool, err string, args ... interface{}) {
  4. if cond {
  5. return
  6. }
  7. test.Errorf(err, args)
  8. }
  9. func TestFloa(t * testing.T) {
  10. f1 := tamias.Float(10.0)
  11. f2 := tamias.Float(20.0)
  12. assert(t, f1 == 10.0, "Float should equal it's initial value 10.0", f1)
  13. assert(t, f1.Equals(10.0), "Float should equal it's initial value 10.0", f1)
  14. assert(t, f1.Min(f2) == f1, "Min must work properly.", f1)
  15. assert(t, f1.Max(f2) == f2, "Max must work properly.", f1)
  16. assert(t, f2.Min(f1) == f1, "Min must work properly in reverse.", f1)
  17. assert(t, f2.Max(f1) == f2, "Max must work properly in reverse.", f1)
  18. }
  19. func TestBB(t * testing.T) {
  20. bb := tamias.BBMake(10.0, 20.0, 40.0, 80.0)
  21. // assert(bb != nil , "Bounds Box must be constructable")
  22. assert(t, bb.L == 10.0, "Bounds Box L must be 10.0", bb.L)
  23. assert(t, bb.T == 20.0, "Bounds Box T must be 20.0", bb.T)
  24. assert(t, bb.R == 40.0, "Bounds Box R must be 40.0", bb.R)
  25. assert(t, bb.B == 80.0, "Bounds Box B must be 80.0", bb.B)
  26. b2 := bb.Grow(10.0)
  27. assert(t, b2.Contains(bb), "Contains and grow work correctly.", bb, b2)
  28. }
  29. func TestShape(t * testing.T) {
  30. body := tamias.BodyNew(10.0, 0.0)
  31. box := tamias.BoxShapeNew(body, 20.0, 30.0)
  32. box.CacheBB(body.Pos(), body.Rot())
  33. assert(t, box.GetBB() != nil, "Box must have a bounds box")
  34. if box.GetBB() != nil {
  35. // the object should have been placed at (0,0), so half the BB
  36. // is positve and half negative
  37. // chipmunk, and hence Tamias too, use a normal Carthesian
  38. // coordinate system where the zero is in the bottom left
  39. assert(t, box.Shape.BB == box.GetBB(), "BB and GetBB() are the same")
  40. assert(t, box.GetBB().L == -10.0, "Box must have BB.L -10.0", box.GetBB().L)
  41. assert(t, box.GetBB().T == 15.0, "Box must have BB.T -15.0", box.GetBB().T)
  42. assert(t, box.GetBB().R == 10.0, "Box must have BB.L 10.0", box.GetBB().R)
  43. assert(t, box.GetBB().B == -15.0, "Box must have BB.T -15.0", box.GetBB().B)
  44. }
  45. }
  46. func TestVect(t * testing.T) {
  47. v1 := tamias.VF(3.0, 4.0)
  48. v2 := tamias.V(1.0, 0.0)
  49. // tamias.V(3.0, 4.0)
  50. assert(t, v1.X == 3.0, "v1.X should be 3.0", v1.X)
  51. assert(t, v1.Y == 4.0, "v1.Y should be 4.0", v1.Y)
  52. assert(t, v2.X == 1.0, "v1.X should be 1.0", v2.X)
  53. assert(t, v2.Y == 0.0, "v1.Y should be 0.0", v2.Y)
  54. assert(t, v1.Length() == 5.0, "Vector length should be 5.")
  55. assert(t, v1.Equals(v1), "Vector should be equal to itself.")
  56. assert(t, v1.Add(v2).X == 4.0, "Vector Sum X should be 4.")
  57. assert(t, v1.Add(v2).Y == 4.0, "Vector Sum X should be 4.")
  58. vm := v1.Mult(tamias.Float(2.0))
  59. assert(t, vm.Y == 8.0, "Vector Mult Y should be 8.0.", vm.X, vm.Y)
  60. }
  61. func TestSpaceMap(t * testing.T) {
  62. sm := tamias.SpaceMapNew(10.0, 25)
  63. body := tamias.BodyNew(10.0, 0.0)
  64. box := tamias.BoxShapeNew(body, 20.0, 30.0)
  65. assert(sm != nil, "SpaceMap should be constructable")
  66. sm.Insert(box)
  67. bb := box.GetBB().Grow(10.0)
  68. found := sm.FindBB(&bb)
  69. assert(t, found != nil, "SpaceMap should find back inserted items.", bb)
  70. if found != nil {
  71. block := func(el interface {})(bool) {
  72. fmt.Println((el.(*tamias.PolyShape)))
  73. return el.(*tamias.PolyShape) == box
  74. }
  75. res := iterable.Find(found, block)
  76. assert(t, res != nil, "SpaceMap should find back the *right* inserted items.", found)
  77. } else {
  78. t.Errorf("SpaceMap could not find insertd item. (%s)\n", sm.String())
  79. // fmt.Printf()
  80. }
  81. }