al_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 := AllegroVersion()
  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 := Time()
  80. Rest(rest)
  81. t2 := Time()
  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 := StandardPath(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 := FindMonitorInfo(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 := NumJoysticks()
  136. t.Logf("Found %d joysticks\n", num)
  137. for index := 0; index < num; index++ {
  138. js := FindJoystick(index)
  139. jsname := js.Name()
  140. sticks := js.NumSticks()
  141. buttons := js.NumButtons()
  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.NumAxes(sdex)
  146. sname := js.StickName(sdex)
  147. sfname := js.StickFlagsName(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.AxisName(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.ButtonName(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.SetWindowTitle("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 full screen display modes functions
  214. func TestDisplayMode(t *testing.T) {
  215. InstallSystem()
  216. defer UninstallSystem()
  217. modes := DisplayModes()
  218. for _, m := range modes {
  219. t.Logf("Display mode: %s\n", m.String());
  220. }
  221. monitors := AllMonitorInfo()
  222. for _, m := range monitors {
  223. t.Logf("Monitor : %s\n", m.String());
  224. }
  225. }
  226. // Test some font functions
  227. func TestFonts(t *testing.T) {
  228. InstallSystem()
  229. defer UninstallSystem()
  230. InitFontAddon()
  231. defer ShutdownFontAddon()
  232. display := makeDisplay()
  233. if display == nil {
  234. t.Error("Error creating display.")
  235. }
  236. font := CreateBuiltinFont()
  237. if font == nil {
  238. t.Error("Cannot create built in font.")
  239. }
  240. ranges , count := font.Ranges()
  241. t.Logf("Built in font has ranges: %v, %d\n", ranges, count);
  242. blue := CreateColor(0.0, 0.0, 1.0, 1.0)
  243. yellow := CreateColor(1.0, 1.0, 0.0, 1.0)
  244. ClearToColor(blue)
  245. 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)
  246. FlipDisplay()
  247. Rest(1.0)
  248. font.Destroy()
  249. display.Destroy()
  250. }
  251. func randomColor() Color {
  252. return CreateColor(rand.Float32(), rand.Float32(), rand.Float32(), 1.0)
  253. }
  254. // Test some bitmap functions
  255. func TestBitmaps(t *testing.T) {
  256. InstallSystem()
  257. defer UninstallSystem()
  258. InitImageAddon()
  259. defer ShutdownImageAddon()
  260. display := makeDisplay()
  261. if display == nil {
  262. t.Error("Error creating display.")
  263. }
  264. bmp := loadBitmap(t, "gin_feather.png")
  265. blue := CreateColor(0.0, 0.0, 1.0, 1.0)
  266. yellow := CreateColor(1.0, 1.0, 0.0, 1.0)
  267. ClearToColor(blue)
  268. bmp.DrawTinted(yellow, 20, 30, 0)
  269. FlipDisplay()
  270. tb := TargetBitmap()
  271. if (!tb.Save(filepath.Join("testdata", "TestBitmaps.out.png"))) {
  272. t.Errorf("Could not save output file.")
  273. }
  274. Rest(1.0)
  275. display.Destroy()
  276. }
  277. // Test some primitive functions
  278. func TestPrimitives(t *testing.T) {
  279. InstallSystem()
  280. defer UninstallSystem()
  281. InitPrimitivesAddon()
  282. defer ShutdownPrimitivesAddon()
  283. display := makeDisplay()
  284. if display == nil {
  285. t.Error("Error creating display.")
  286. }
  287. blue := CreateColor(0.0, 0.0, 1.0, 1.0)
  288. yellow := CreateColor(1.0, 1.0, 0.0, 1.0)
  289. ClearToColor(blue)
  290. DrawLine(0,0,640,480, yellow, 1)
  291. DrawCircle(50,50,10, randomColor(), 3.0)
  292. DrawEllipse(70,70,20,30, randomColor(), 5.0)
  293. DrawFilledRoundedRectangle(120, 120, 300, 300, 7, 5, randomColor())
  294. FlipDisplay()
  295. Rest(1.0)
  296. display.Destroy()
  297. }
  298. // Test some bitmap functions
  299. func TestPrimitives2(t *testing.T) {
  300. InstallSystem()
  301. defer UninstallSystem()
  302. InitImageAddon()
  303. defer ShutdownImageAddon()
  304. InitPrimitivesAddon()
  305. defer ShutdownPrimitivesAddon()
  306. display := makeDisplay()
  307. if display == nil {
  308. t.Error("Error creating display.")
  309. }
  310. bmp := loadBitmap(t, "gin_feather.png")
  311. blue := CreateColor(0.0, 0.0, 1.0, 1.0)
  312. bg := CreateColor(1.0, 1.0, 1.0, 1.0)
  313. ClearToColor(blue)
  314. v := make([]Vertex, 3)
  315. v[0].Init(10.0, 20.0, 0.0, 0.0 * bmp.Widthf(), 0.0, bg)
  316. v[1].Init(10.0, 120.0, 0.0, 1.0 * bmp.Widthf(), 0.0, bg)
  317. v[2].Init(120.0, 120.0, 0.0, 0.0 * bmp.Widthf(), 1.0 * bmp.Heightf(), bg)
  318. DrawPrim(v, bmp, 0, 3, PRIM_TRIANGLE_LIST)
  319. FlipDisplay()
  320. Rest(1.0)
  321. display.Destroy()
  322. }
  323. // Test some native dialogs
  324. func TestDialogs(t *testing.T) {
  325. InstallSystem()
  326. defer UninstallSystem()
  327. InitNativeDialogAddon()
  328. defer ShutdownNativeDialogAddon()
  329. display := makeDisplay()
  330. if display == nil {
  331. t.Error("Error creating display.")
  332. }
  333. fc := CreateNativeFileDialog(".","title","*", FILECHOOSER_FILE_MUST_EXIST | FILECHOOSER_MULTIPLE)
  334. fc.Show(display)
  335. cn := fc.Count()
  336. for i:= 0; i < cn ; i ++ {
  337. fn := fc.Path(i)
  338. t.Logf("File %d of %d selected: %s\n", i + 1, cn, fn)
  339. }
  340. tl := CreateNativeTextLog("title", TEXTLOG_MONOSPACE)
  341. tl.Append("append")
  342. res := display.ShowNativeMessageBox("title", "heading", "text", "buttons", MESSAGEBOX_WARN)
  343. t.Logf("Dialog result: %d\n", res)
  344. FlipDisplay()
  345. Rest(1.0)
  346. tl.Close()
  347. fc.Destroy()
  348. display.Destroy()
  349. }
  350. // Benchmark basic display function ClearToColor
  351. func BenchmarkClearToColor(b *testing.B) {
  352. b.StopTimer()
  353. InstallSystem()
  354. defer UninstallSystem()
  355. display := makeDisplay()
  356. blue := CreateColor(0.0, 0.0, 1.0, 1.0)
  357. if display == nil {
  358. b.Fatal("Error creating display. Cannot benchmark it.")
  359. }
  360. b.StartTimer()
  361. for i := 0; i < b.N; i++ {
  362. ClearToColor(blue)
  363. }
  364. // FlipDisplay()
  365. display.Destroy()
  366. }
  367. // Benchmark basic display function FlipDisplay
  368. func BenchmarkFlipDisplay(b *testing.B) {
  369. b.StopTimer()
  370. InstallSystem()
  371. defer UninstallSystem()
  372. display := makeDisplay()
  373. if display == nil {
  374. b.Fatal("Error creating display. Cannot benchmark it.")
  375. }
  376. b.StartTimer()
  377. for i := 0; i < b.N; i++ {
  378. FlipDisplay()
  379. }
  380. display.Destroy()
  381. }
  382. // Benchmarking of C call overhead
  383. func BenchmarkDoNothing(b *testing.B) {
  384. for i := 0; i < b.N; i++ {
  385. DoNothing()
  386. }
  387. }