al_test.go 7.5 KB

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