al_test.go 9.2 KB

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