21 changed files with 236 additions and 114 deletions
@ -0,0 +1,49 @@
@@ -0,0 +1,49 @@
|
||||
package gohan |
||||
|
||||
import "testing" |
||||
|
||||
var testComponentID = NewComponentID() |
||||
|
||||
type testComponent struct { |
||||
X, Y float64 |
||||
} |
||||
|
||||
func (t testComponent) ComponentID() ComponentID { |
||||
return testComponentID |
||||
} |
||||
|
||||
func BenchmarkComponent(b *testing.B) { |
||||
e := NewEntity() |
||||
|
||||
e.AddComponent(&testComponent{ |
||||
X: 108, |
||||
Y: 0, |
||||
}) |
||||
|
||||
b.StopTimer() |
||||
b.ResetTimer() |
||||
b.ReportAllocs() |
||||
b.StartTimer() |
||||
|
||||
for i := 0; i < b.N; i++ { |
||||
_ = e.Component(testComponentID) |
||||
} |
||||
} |
||||
|
||||
func BenchmarkAddComponent(b *testing.B) { |
||||
e := NewEntity() |
||||
|
||||
c := &testComponent{ |
||||
X: 108, |
||||
Y: 0, |
||||
} |
||||
|
||||
b.StopTimer() |
||||
b.ResetTimer() |
||||
b.ReportAllocs() |
||||
b.StartTimer() |
||||
|
||||
for i := 0; i < b.N; i++ { |
||||
e.AddComponent(c) |
||||
} |
||||
} |
@ -0,0 +1,60 @@
@@ -0,0 +1,60 @@
|
||||
package gohan |
||||
|
||||
import ( |
||||
"testing" |
||||
"time" |
||||
) |
||||
|
||||
func TestActiveEntities(t *testing.T) { |
||||
t.Parallel() |
||||
|
||||
active := ActiveEntities() |
||||
if active != 0 { |
||||
t.Fatalf("expected 0 active entities, got %d", active) |
||||
} |
||||
|
||||
wait() |
||||
active = ActiveEntities() |
||||
if active != 0 { |
||||
t.Fatalf("expected 0 active entities, got %d", active) |
||||
} |
||||
|
||||
// Create entity.
|
||||
e1 := NewEntity() |
||||
|
||||
wait() |
||||
active = ActiveEntities() |
||||
if active != 1 { |
||||
t.Fatalf("expected 1 active entities, got %d", active) |
||||
} |
||||
|
||||
// Create entity.
|
||||
e2 := NewEntity() |
||||
|
||||
wait() |
||||
active = ActiveEntities() |
||||
if active != 2 { |
||||
t.Fatalf("expected 2 active entities, got %d", active) |
||||
} |
||||
|
||||
e1.Remove() |
||||
|
||||
wait() |
||||
active = ActiveEntities() |
||||
if active != 1 { |
||||
t.Fatalf("expected 1 active entities, got %d", active) |
||||
} |
||||
|
||||
e2.Remove() |
||||
|
||||
wait() |
||||
active = ActiveEntities() |
||||
if active != 0 { |
||||
t.Fatalf("expected 0 active entities, got %d", active) |
||||
} |
||||
} |
||||
|
||||
// wait causes the program to wait long enough to expire all duration-based caches.
|
||||
func wait() { |
||||
time.Sleep(2 * time.Second) |
||||
} |
Loading…
Reference in new issue