al_test.go 8.2 KB

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