al_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. // Test some bitmap functions
  286. func TestPrimitives2(t *testing.T) {
  287. InstallSystem()
  288. defer UninstallSystem()
  289. InitImageAddon()
  290. defer ShutdownImageAddon()
  291. InitPrimitivesAddon()
  292. defer ShutdownPrimitivesAddon()
  293. display := makeDisplay()
  294. if display == nil {
  295. t.Error("Error creating display.")
  296. }
  297. bmp := loadBitmap(t, "gin_feather.png")
  298. blue := CreateColor(0.0, 0.0, 1.0, 1.0)
  299. bg := CreateColor(1.0, 1.0, 1.0, 1.0)
  300. ClearToColor(blue)
  301. v := make([]Vertex, 3)
  302. v[0].Init(10.0, 20.0, 0.0, 0.0 * bmp.Widthf(), 0.0, bg)
  303. v[1].Init(10.0, 120.0, 0.0, 1.0 * bmp.Widthf(), 0.0, bg)
  304. v[2].Init(120.0, 120.0, 0.0, 0.0 * bmp.Widthf(), 1.0 * bmp.Heightf(), bg)
  305. DrawPrim(v, bmp, 0, 3, PRIM_TRIANGLE_LIST)
  306. FlipDisplay()
  307. Rest(1.0)
  308. display.Destroy()
  309. }
  310. // Test some native dialogs
  311. func TestDialogs(t *testing.T) {
  312. InstallSystem()
  313. defer UninstallSystem()
  314. InitNativeDialogAddon()
  315. defer ShutdownNativeDialogAddon()
  316. display := makeDisplay()
  317. if display == nil {
  318. t.Error("Error creating display.")
  319. }
  320. fc := CreateNativeFileDialog(".","title","*", FILECHOOSER_FILE_MUST_EXIST | FILECHOOSER_MULTIPLE)
  321. fc.Show(display)
  322. cn := fc.Count()
  323. for i:= 0; i < cn ; i ++ {
  324. fn := fc.Path(i)
  325. t.Logf("File %d of %d selected: %s\n", i + 1, cn, fn)
  326. }
  327. tl := CreateNativeTextLog("title", TEXTLOG_MONOSPACE)
  328. tl.Append("append")
  329. res := display.ShowNativeMessageBox("title", "heading", "text", "buttons", MESSAGEBOX_WARN)
  330. t.Logf("Dialog result: %d\n", res)
  331. FlipDisplay()
  332. Rest(1.0)
  333. tl.Close()
  334. fc.Destroy()
  335. display.Destroy()
  336. }
  337. // Benchmark basic display function ClearToColor
  338. func BenchmarkClearToColor(b *testing.B) {
  339. b.StopTimer()
  340. InstallSystem()
  341. defer UninstallSystem()
  342. display := makeDisplay()
  343. blue := CreateColor(0.0, 0.0, 1.0, 1.0)
  344. if display == nil {
  345. b.Fatal("Error creating display. Cannot benchmark it.")
  346. }
  347. b.StartTimer()
  348. for i := 0; i < b.N; i++ {
  349. ClearToColor(blue)
  350. }
  351. // FlipDisplay()
  352. display.Destroy()
  353. }
  354. // Benchmark basic display function FlipDisplay
  355. func BenchmarkFlipDisplay(b *testing.B) {
  356. b.StopTimer()
  357. InstallSystem()
  358. defer UninstallSystem()
  359. display := makeDisplay()
  360. if display == nil {
  361. b.Fatal("Error creating display. Cannot benchmark it.")
  362. }
  363. b.StartTimer()
  364. for i := 0; i < b.N; i++ {
  365. FlipDisplay()
  366. }
  367. display.Destroy()
  368. }
  369. // Benchmarking of C call overhead
  370. func BenchmarkDoNothing(b *testing.B) {
  371. for i := 0; i < b.N; i++ {
  372. DoNothing()
  373. }
  374. }