This repository has been archived by the owner on Apr 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-gui.go
179 lines (148 loc) · 3.45 KB
/
test-gui.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import "fmt"
import "os"
import "time"
import "fungo/sdl"
// import "fungo/draw"
import "fungo/gui"
import "reflect"
// import "rand"
// import "tamias"
// import "fungo/sdl"
/*
import "exp/draw"
import "exp/draw/x11"
*/
type testbool bool
type testresults struct {
ok, failed, total int
}
var suite = testresults{0,0,0}
func sleep(secs int) {
time.Sleep(int64(secs) * 1000000000);
}
func (cond testbool) should(err string, args ...) {
assert(bool(cond), err, args)
}
func error(error string, args ...) {
suite.failed++
suite.total++
fmt.Fprintln(os.Stderr, "Failed assertion nr", suite.total, ":", error, args);
}
func no_error() {
suite.ok++
suite.total++
}
func assert(cond bool, err string, args ...) {
if cond {
no_error()
return
}
error(err, args)
}
func TestResults() {
fmt.Fprintf(os.Stderr, "Test results: %d/%d test passed, %d/%d failed.\n",
suite.ok, suite.total, suite.failed, suite.total)
}
// This test doesn't score anything, just prints results, which should print.
//Tests the GUI
func TestGui() {
screen := sdl.OpenScreen(640, 480, 32, 0)
sdl.EnableUnicode(1)
/*
font := sdl.LoadTTFont("data/sazanami-gothic.ttf", 24)
defer font.Free()
img := font.RenderBlended("Hello 世界!", white)
defer img.Free()
*/
// open the screen
gui := gui.NewHanao(screen)
for !gui.Done() {
gui.Update()
/*
gui.Draw(screen)
*/
screen.Flip()
}
}
type Foo struct {
}
type Any interface{}
type Message string
type Method reflect.Method
type Object interface {
Send(Message , ...Any)
DefineMethod(Message, Any)
GetMethod(Message, Any)
}
func Hello() {
fmt.Println("Hello Dynamic Call")
}
func Hello2(extra string) {
fmt.Println("Hello Dynamic Call", extra)
}
func Square(val int) (int) {
return val * val
}
// Can call any function with any arguments
// Slow but flexible.
// TODO: Think about what to do with the return values.
// A raw []reflect.Value is not so interesting
func DynamicCall(fun Any, args ...Any) (Any) {
fval, ok := reflect.NewValue(fun).(*reflect.FuncValue)
if ! ok { error("Not a function") ; return nil }
// fmt.Println(fval)
funkind, ok2 := fval.Type().(*reflect.FuncType)
if ! ok2 { error("Not a function") ; return nil }
// fmt.Println(funkind, funkind.Name(), funkind.String(), funkind.NumIn())
nargs := funkind.NumIn()
vargs := make([]reflect.Value, nargs)
if len(args) < nargs { error("Too few arguments") ; return nil }
for i:= 0 ; i < nargs ; i ++ {
val := reflect.NewValue(args[i])
if val.Type() != funkind.In(i) {
error("Wrong argument type: ", val.Type(),
"expected", funkind.In(i) )
return nil
}
vargs[i] = val
}
results := fval.Call(vargs)
return results
}
func TimeFunction(repeats int, totime func()) (int64) {
start := time.Nanoseconds()
for i:= 0 ; i < repeats; i++ {
totime()
}
stop := time.Nanoseconds()
return stop - start
}
// Tests the object system
func TestObject() {
DynamicCall(Hello, "Ignore me", "Ignore me too")
DynamicCall(Hello2, 1)
Hello2("World")
tf1 := func() {
DynamicCall(Square, 12345)
}
tf2 := func() {
Square(12345)
}
repeats := 100000
t1 := TimeFunction(repeats, tf1)
t2 := TimeFunction(repeats, tf2)
fmt.Println(repeats, t1, t2)
}
func TestSetup() {
sdl.InitDefault()
}
func TestQuit() {
sdl.Quit()
}
func main() {
TestSetup()
// TestObject()
TestGui()
TestResults()
}