al_test.go 5.8 KB

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