123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426 |
- package al
- import "testing"
- import "runtime"
- import "flag"
- import "path/filepath"
- import "math/rand"
- // some parameters
- const expected_version = 84017665 // 83952897
- const SCREEN_W = 640
- const SCREEN_H = 480
- const TEST_FULLSCREEN = true
- var fullscreen = flag.Bool("fullscreen", false, "Run fullscreen or not")
- func TestGetAllegroVersion(t *testing.T) {
- version := GetAllegroVersion()
- if version != expected_version {
- t.Errorf("unexpected version of Allegro: %d in stead of %d!",
- version, expected_version)
- }
- }
- const CALLBACK_RESULT = 77
- const BMP_W = 11
- const BMP_H = 23
- // Helper that loads bitmaps from the testdata folder
- func loadBitmap(t *testing.T, name string) * Bitmap {
- path := filepath.Join("testdata", name) // relative path
- bmp := LoadBitmap(path)
- if bmp == nil {
- t.Fatalf("Could not load bitmap: %s: %d\n", path, Errno())
- }
- return bmp
- }
- // Test C callbacks, for example create bitmap
- // Test system installation and deinstallation
- func TestSystemInstall(t *testing.T) {
- if IsSystemInstalled() {
- t.Errorf("System should not be installed before install\n")
- return
- }
- if !InstallSystem() {
- t.Errorf("System should be installed now\n")
- return
- }
- if !IsSystemInstalled() {
- t.Errorf("System should be installed after install\n")
- return
- }
- UninstallSystem()
- if IsSystemInstalled() {
- t.Errorf("System should not be installed after uninstall\n")
- return
- }
- }
- // Test USTR
- func TestUSTR(t *testing.T) {
- s1 := "Hello no unicode!"
- s2 := "Hello µ unicode!"
- u1 := USTRV(s1)
- u2 := USTRV(s2)
- r1 := u1.String()
- r2 := u2.String()
- if s1 != r1 {
- t.Errorf("USTR roundtrip failed: %s->%s", s1, r1)
- }
- if s2 != r2 {
- t.Errorf("USTR roundtrip failed: %s->%s", s2, r2)
- }
- u1.Free()
- u1.Free()
- u1.Free()
- if u1.String() != "<destroyed>" {
- t.Error("USTR.String() should return <destroyed> after Free()")
- }
- }
- // Test timer functions
- func TestGetTimeRest(t *testing.T) {
- InstallSystem()
- defer UninstallSystem()
- rest := 0.123
- t1 := GetTime()
- Rest(rest)
- t2 := GetTime()
- del := t2 - t1 - rest
- if (del > 0.001) || (del < -0.001) {
- t.Errorf("Rest/GetTime for %f not precise %f %f %f", rest, t1, t2, del)
- }
- }
- // Test path functions
- func TestPath(t *testing.T) {
- InstallSystem()
- defer UninstallSystem()
- path := GetStandardPath(TEMP_PATH)
- str := path.String()
- tmp := "/tmp/"
- // special case for windows...
- if runtime.GOOS == "windows" {
- tmp = `C:\TMP\`
- }
- if str != tmp {
- t.Errorf("GetStandardPath(TEMP_PATH) is not %s but %s", tmp, str)
- }
- }
- // Test display info
- func TestGetInfo(t *testing.T) {
- InstallSystem()
- defer UninstallSystem()
- nv := NumVideoAdapters()
- if nv < 1 {
- t.Error("No video adapters found!")
- }
- for index := 0; index < nv; index++ {
- info := GetMonitorInfo(index)
- if info == nil {
- t.Errorf("Video adapter %d not found!", index)
- continue
- }
- t.Logf("MonitorInfo for %d: %d %d %d %d\n", index,
- info.X1(), info.Y1(), info.X2(), info.Y2())
- }
- }
- // Test screen saver inhibition.
- func TestInhibitScreensaver(t *testing.T) {
- InstallSystem()
- defer UninstallSystem()
- ok := InhibitScreensaver(true)
- if !ok {
- t.Errorf("InhibitScreensaver failed: %v", ok)
- }
- }
- // Test joystick functions, works better with a joystick plugged in ;)
- func TestJoystick(t *testing.T) {
- InstallSystem()
- defer UninstallSystem()
- InstallJoystick()
- defer UninstallJoystick()
- num := GetNumJoysticks()
- t.Logf("Found %d joysticks\n", num)
- for index := 0; index < num; index++ {
- js := GetJoystick(index)
- jsname := js.GetName()
- sticks := js.GetNumSticks()
- buttons := js.GetNumButtons()
- t.Logf("Joystick %s (nr %d) has %d sticks and %d buttons:\n",
- jsname, index, sticks, buttons)
- for sdex := 0; sdex < sticks; sdex++ {
- axes := js.GetNumAxes(sdex)
- sname := js.GetStickName(sdex)
- sfname := js.GetStickFlagsName(sdex)
- t.Logf("Stick %s (nr %d, %s) has %d axes: ", sname, sdex, sfname, axes)
- for adex := 0; adex < axes; adex++ {
- aname := js.GetAxisName(sdex, adex)
- t.Logf("%s (nr %d) ", aname, adex)
- }
- }
- t.Logf("\nButtons :")
- for bdex := 0; bdex < buttons; bdex++ {
- bname := js.GetButtonName(bdex)
- t.Logf("%s (nr %d) ", bname, bdex)
- }
- t.Logf("\n")
- }
- }
- // Makesa display for testing, using the test's setting above
- func makeDisplay() *Display {
- flags := 0
- // Use full screen mode if needed.
- if *fullscreen {
- flags = FULLSCREEN // | GENERATE_EXPOSE_EVENTS
- } else {
- SetNewDisplayFlags(flags)
- }
- // Create a window to display things on: 640x480 pixels.
- display := CreateDisplay(SCREEN_W, SCREEN_H)
- display.Resize(SCREEN_W, SCREEN_H)
- if !(*fullscreen) {
- display.SetTitle("Algo test window")
- }
- return display
- }
- // Test basic display functions
- func TestBasicDisplay(t *testing.T) {
- InstallSystem()
- defer UninstallSystem()
- display := makeDisplay()
- if display == nil {
- t.Error("Error creating display.")
- }
- HoldBitmapDrawing(true)
- if !IsBitmapDrawingHeld() {
- t.Error("Bitmap drawing hold failed")
- }
- HoldBitmapDrawing(false)
- if IsBitmapDrawingHeld() {
- t.Error("Bitmap drawing hold release failed")
- }
- /*
- if ! {
- t.Error("Resize of display failed.")
- }
- */
- blue := CreateColor(0.0, 0.0, 1.0, 1.0)
- yellow := CreateColor(1.0, 1.0, 0.0, 1.0)
- ClearToColor(blue)
- DrawPixel(20.0, 10.0, yellow)
- FlipDisplay()
- Rest(1.0)
- display.SetWindowPosition(50, 100)
- ClearToColor(yellow)
- DrawPixel(20.0, 10.0, blue)
- FlipDisplay()
- display.Destroy()
- Rest(1.0)
- }
- // Test some font functions
- func TestFonts(t *testing.T) {
- InstallSystem()
- defer UninstallSystem()
- InitFontAddon()
- defer ShutdownFontAddon()
-
- display := makeDisplay()
- if display == nil {
- t.Error("Error creating display.")
- }
-
-
- font := CreateBuiltinFont()
- if font == nil {
- t.Error("Cannot create built in font.")
- }
- ranges , count := font.Ranges()
- t.Logf("Built in font has ranges: %v, %d\n", ranges, count);
- blue := CreateColor(0.0, 0.0, 1.0, 1.0)
- yellow := CreateColor(1.0, 1.0, 0.0, 1.0)
- ClearToColor(blue)
- 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)
- FlipDisplay()
- Rest(1.0)
- font.Destroy()
- display.Destroy()
- }
- func randomColor() Color {
- return CreateColor(rand.Float32(), rand.Float32(), rand.Float32(), 1.0)
- }
- // Test some bitmap functions
- func TestBitmaps(t *testing.T) {
- InstallSystem()
- defer UninstallSystem()
- InitImageAddon()
- defer ShutdownImageAddon()
-
- display := makeDisplay()
- if display == nil {
- t.Error("Error creating display.")
- }
-
- bmp := loadBitmap(t, "gin_feather.png")
- blue := CreateColor(0.0, 0.0, 1.0, 1.0)
- yellow := CreateColor(1.0, 1.0, 0.0, 1.0)
- ClearToColor(blue)
- bmp.DrawTinted(yellow, 20, 30, 0)
- FlipDisplay()
- tb := TargetBitmap()
- if (!tb.Save(filepath.Join("testdata", "TestBitmaps.out.png"))) {
- t.Errorf("Could not save output file.")
- }
- Rest(1.0)
- display.Destroy()
- }
- // Test some primitive functions
- func TestPrimitives(t *testing.T) {
- InstallSystem()
- defer UninstallSystem()
- InitPrimitivesAddon()
- defer ShutdownPrimitivesAddon()
-
- display := makeDisplay()
- if display == nil {
- t.Error("Error creating display.")
- }
- blue := CreateColor(0.0, 0.0, 1.0, 1.0)
- yellow := CreateColor(1.0, 1.0, 0.0, 1.0)
- ClearToColor(blue)
- DrawLine(0,0,640,480, yellow, 1)
- DrawCircle(50,50,10, randomColor(), 3.0)
- DrawEllipse(70,70,20,30, randomColor(), 5.0)
- DrawFilledRoundedRectangle(120, 120, 300, 300, 7, 5, randomColor())
- FlipDisplay()
- Rest(1.0)
- display.Destroy()
- }
- // Test some bitmap functions
- func TestPrimitives2(t *testing.T) {
- InstallSystem()
- defer UninstallSystem()
- InitImageAddon()
- defer ShutdownImageAddon()
- InitPrimitivesAddon()
- defer ShutdownPrimitivesAddon()
-
- display := makeDisplay()
- if display == nil {
- t.Error("Error creating display.")
- }
-
- bmp := loadBitmap(t, "gin_feather.png")
- blue := CreateColor(0.0, 0.0, 1.0, 1.0)
- bg := CreateColor(1.0, 1.0, 1.0, 1.0)
- ClearToColor(blue)
- v := make([]Vertex, 3)
-
- v[0].Init(10.0, 20.0, 0.0, 0.0 * bmp.Widthf(), 0.0, bg)
- v[1].Init(10.0, 120.0, 0.0, 1.0 * bmp.Widthf(), 0.0, bg)
- v[2].Init(120.0, 120.0, 0.0, 0.0 * bmp.Widthf(), 1.0 * bmp.Heightf(), bg)
-
- DrawPrim(v, bmp, 0, 3, PRIM_TRIANGLE_LIST)
- FlipDisplay()
- Rest(1.0)
- display.Destroy()
- }
- // Test some native dialogs
- func TestDialogs(t *testing.T) {
- InstallSystem()
- defer UninstallSystem()
- InitNativeDialogAddon()
- defer ShutdownNativeDialogAddon()
-
-
- display := makeDisplay()
- if display == nil {
- t.Error("Error creating display.")
- }
-
- fc := CreateNativeFileDialog(".","title","*", FILECHOOSER_FILE_MUST_EXIST | FILECHOOSER_MULTIPLE)
- fc.Show(display)
- cn := fc.Count()
-
- for i:= 0; i < cn ; i ++ {
- fn := fc.Path(i)
- t.Logf("File %d of %d selected: %s\n", i + 1, cn, fn)
- }
-
- tl := CreateNativeTextLog("title", TEXTLOG_MONOSPACE)
- tl.Append("append")
-
- res := display.ShowNativeMessageBox("title", "heading", "text", "buttons", MESSAGEBOX_WARN)
- t.Logf("Dialog result: %d\n", res)
-
-
- FlipDisplay()
- Rest(1.0)
- tl.Close()
- fc.Destroy()
- display.Destroy()
- }
- // Benchmark basic display function ClearToColor
- func BenchmarkClearToColor(b *testing.B) {
- b.StopTimer()
- InstallSystem()
- defer UninstallSystem()
- display := makeDisplay()
- blue := CreateColor(0.0, 0.0, 1.0, 1.0)
- if display == nil {
- b.Fatal("Error creating display. Cannot benchmark it.")
- }
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- ClearToColor(blue)
- }
- // FlipDisplay()
- display.Destroy()
- }
- // Benchmark basic display function FlipDisplay
- func BenchmarkFlipDisplay(b *testing.B) {
- b.StopTimer()
- InstallSystem()
- defer UninstallSystem()
- display := makeDisplay()
- if display == nil {
- b.Fatal("Error creating display. Cannot benchmark it.")
- }
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- FlipDisplay()
- }
- display.Destroy()
- }
- // Benchmarking of C call overhead
- func BenchmarkDoNothing(b *testing.B) {
- for i := 0; i < b.N; i++ {
- DoNothing()
- }
- }
|