al_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package al
  2. import "testing"
  3. import "runtime"
  4. // import "fmt"
  5. // some parameters
  6. const expected_version = 83887873
  7. const SCREEN_W = 640
  8. const SCREEN_H = 480
  9. const TEST_FULLSCREEN = false
  10. func TestGetAllegroVersion(t *testing.T) {
  11. version := GetAllegroVersion()
  12. if version != expected_version {
  13. t.Errorf("unexpected version of Allegro: %d in stead of %d!",
  14. version, expected_version)
  15. }
  16. }
  17. // Test system installation and deinstallation
  18. func TestSystemInstall(t *testing.T) {
  19. if IsSystemInstalled() {
  20. t.Errorf("System should not be installed before install\n")
  21. return
  22. }
  23. InstallSystem()
  24. if !IsSystemInstalled() {
  25. t.Errorf("System should be installed after install\n")
  26. return
  27. }
  28. UninstallSystem()
  29. if IsSystemInstalled() {
  30. t.Errorf("System should not be installed after uninstall\n")
  31. return
  32. }
  33. }
  34. // Test USTR
  35. func TestUSTR(t *testing.T) {
  36. s1 := "Hello no unicode!"
  37. s2 := "Hello µ unicode!"
  38. u1 := USTRV(s1)
  39. u2 := USTRV(s2)
  40. r1 := u1.String()
  41. r2 := u2.String()
  42. if s1 != r1 {
  43. t.Errorf("USTR roundtrip failed: %s->%s", s1, r1)
  44. }
  45. if s2 != r2 {
  46. t.Errorf("USTR roundtrip failed: %s->%s", s2, r2)
  47. }
  48. u1.Free()
  49. u1.Free()
  50. u1.Free()
  51. if u1.String() != "<destroyed>" {
  52. t.Error("USTR.String() should return <destroyed> after Free()")
  53. }
  54. }
  55. // Test timer functions
  56. func TestGetTimeRest(t *testing.T) {
  57. InstallSystem()
  58. defer UninstallSystem()
  59. rest := 0.123
  60. t1 := GetTime()
  61. Rest(rest)
  62. t2 := GetTime()
  63. del := t2 - t1 - rest
  64. if (del > 0.001) || (del < -0.001) {
  65. t.Errorf("Rest/GetTime for %f not precise %f %f %f", rest, t1, t2, del)
  66. }
  67. }
  68. // Test path functions
  69. func TestPath(t *testing.T) {
  70. InstallSystem()
  71. defer UninstallSystem()
  72. path := GetStandardPath(TEMP_PATH)
  73. str := path.String()
  74. tmp := "/tmp/"
  75. // special case for windows...
  76. if runtime.GOOS == "windows" {
  77. tmp = `C:\TMP\`
  78. }
  79. if str != tmp {
  80. t.Errorf("GetStandardPath(TEMP_PATH) is not %s but %s", tmp, str)
  81. }
  82. }
  83. // Test screen saver inhibition.
  84. func TestInhibitScreensaver(t *testing.T) {
  85. InstallSystem()
  86. defer UninstallSystem()
  87. ok := InhibitScreensaver(true)
  88. if !ok {
  89. t.Errorf("InhibitScreensaver failed: %v", ok)
  90. }
  91. }
  92. // Test joystick functions, works better with a joystick plugged in ;)
  93. func TestJoystick(t *testing.T) {
  94. InstallSystem()
  95. defer UninstallSystem()
  96. InstallJoystick()
  97. defer UninstallJoystick()
  98. num := GetNumJoysticks()
  99. t.Logf("Found %d joysticks\n", num)
  100. for index :=0 ; index < num ; index ++ {
  101. js := GetJoystick(index)
  102. jsname := js.GetName()
  103. sticks := js.GetNumSticks()
  104. buttons := js.GetNumButtons()
  105. t.Logf("Joystick %s (nr %d) has %d sticks and %d buttons:\n",
  106. jsname, index, sticks, buttons)
  107. for sdex := 0 ; sdex < sticks ; sdex++ {
  108. axes := js.GetNumAxes(sdex)
  109. sname := js.GetStickName(sdex)
  110. sfname := js.GetStickFlagsName(sdex)
  111. t.Logf("Stick %s (nr %d, %s) has %d axes: ", sname, sdex, sfname, axes)
  112. for adex :=0 ; adex < axes ; adex++ {
  113. aname := js.GetAxisName(sdex, adex)
  114. t.Logf("%s (nr %d) ",aname, adex)
  115. }
  116. }
  117. t.Logf("\nButtons :")
  118. for bdex := 0 ; bdex < buttons; bdex++ {
  119. bname := js.GetButtonName(bdex);
  120. t.Logf("%s (nr %d) ",bname, bdex)
  121. }
  122. t.Logf("\n")
  123. }
  124. }
  125. // Makesa display for testing, using the test's setting above
  126. func makeDisplay() (*Display) {
  127. flags := 0
  128. // Use full screen mode if needed.
  129. if TEST_FULLSCREEN {
  130. flags = FULLSCREEN // | GENERATE_EXPOSE_EVENTS
  131. }
  132. SetNewDisplayFlags(flags)
  133. // Create a window to display things on: 640x480 pixels.
  134. display := CreateDisplay(SCREEN_W, SCREEN_H)
  135. display.Resize(SCREEN_W, SCREEN_H)
  136. return display
  137. }
  138. // Test basic display functions
  139. func TestBasicDisplay(t *testing.T) {
  140. InstallSystem()
  141. defer UninstallSystem()
  142. display := makeDisplay()
  143. if display == nil {
  144. t.Error("Error creating display.")
  145. }
  146. /*
  147. if ! {
  148. t.Error("Resize of display failed.")
  149. }
  150. */
  151. blue := CreateColor(0.0, 0.0, 1.0, 1.0)
  152. yellow := CreateColor(1.0, 1.0, 0.0, 1.0)
  153. ClearToColor(blue)
  154. DrawPixel(20.0, 10.0, yellow)
  155. FlipDisplay()
  156. Rest(1.0)
  157. ClearToColor(yellow)
  158. DrawPixel(20.0, 10.0, blue)
  159. FlipDisplay()
  160. display.Destroy()
  161. Rest(1.0)
  162. }
  163. // Benchmark basic display function ClearToColor
  164. func BenchmarkClearToColor(b *testing.B) {
  165. b.StopTimer()
  166. InstallSystem()
  167. defer UninstallSystem()
  168. display := makeDisplay()
  169. blue := CreateColor(0.0, 0.0, 1.0, 1.0)
  170. if display == nil {
  171. b.Fatal("Error creating display. Cannot benchmark it.")
  172. }
  173. b.StartTimer()
  174. for i := 0; i < b.N; i++ {
  175. ClearToColor(blue)
  176. FlipDisplay()
  177. }
  178. display.Destroy()
  179. }
  180. // Benchmark basic display function FlipDisplay
  181. func BenchmarkFlipDisplay(b *testing.B) {
  182. b.StopTimer()
  183. InstallSystem()
  184. defer UninstallSystem()
  185. display := makeDisplay()
  186. if display == nil {
  187. b.Fatal("Error creating display. Cannot benchmark it.")
  188. }
  189. b.StartTimer()
  190. for i := 0; i < b.N; i++ {
  191. FlipDisplay()
  192. }
  193. display.Destroy()
  194. }
  195. // Benchmarking of C call overhead
  196. // Benchmark basic display function FlipDisplay
  197. func BenchmarkDoNothing(b *testing.B) {
  198. for i := 0; i < b.N; i++ {
  199. DoNothing()
  200. }
  201. }