Skip to content

Commit

Permalink
Add more tests for plots and window (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
mlange-42 committed Nov 8, 2023
1 parent 318673d commit 3311d41
Show file tree
Hide file tree
Showing 12 changed files with 454 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

* Enable full tests with window creation using `xvfb` (#45)
* Add test coverage report to CI, add coveralls badge (#45)
* Add more tests for utility functions and different plots configurations (#46)

## [[v0.5.1]](https://github.com/mlange-42/arche-pixel/compare/v0.5.0...v0.5.1)

Expand Down
32 changes: 32 additions & 0 deletions plot/bars_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package plot_test

import (
"testing"

"github.com/mlange-42/arche-model/model"
"github.com/mlange-42/arche-model/system"
"github.com/mlange-42/arche-pixel/plot"
"github.com/mlange-42/arche-pixel/window"
"github.com/stretchr/testify/assert"
)

func ExampleBars() {
Expand Down Expand Up @@ -37,3 +40,32 @@ func ExampleBars() {

// Output:
}

func TestBars_Columns(t *testing.T) {
m := model.New()
m.TPS = 300
m.AddUISystem((&window.Window{}).
With(&plot.Bars{
Observer: &RowObserver{},
YLim: [...]float64{0, 4},
Columns: []string{"A", "C"},
}))
m.AddSystem(&system.FixedTermination{
Steps: 100,
})
m.Run()
}

func TestBars_PanicColumns(t *testing.T) {
m := model.New()
m.TPS = 300
m.AddUISystem((&window.Window{}).
With(&plot.Bars{
Observer: &RowObserver{},
Columns: []string{"A", "F"},
}))
m.AddSystem(&system.FixedTermination{
Steps: 100,
})
assert.Panics(t, m.Run)
}
21 changes: 21 additions & 0 deletions plot/contour_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package plot_test

import (
"testing"

"github.com/mlange-42/arche-model/model"
"github.com/mlange-42/arche-model/observer"
"github.com/mlange-42/arche-model/system"
Expand Down Expand Up @@ -41,3 +43,22 @@ func ExampleContour() {

// Output:
}

func TestContour_NoLevels(t *testing.T) {
m := model.New()
m.TPS = 300
m.FPS = 0

m.AddUISystem(
(&window.Window{}).
With(&plot.Contour{
Observer: observer.MatrixToGrid(&MatrixObserver{}, nil, nil),
Palette: palette.Heat(16, 1),
}))

m.AddSystem(&system.FixedTermination{
Steps: 100,
})

m.Run()
}
19 changes: 19 additions & 0 deletions plot/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package plot_test

import (
"math"
"testing"

"github.com/mazznoer/colorgrad"
"github.com/mlange-42/arche-model/model"
Expand Down Expand Up @@ -48,6 +49,24 @@ func ExampleImage() {
// Output:
}

func TestImage_LimitsScale(t *testing.T) {
m := model.New()
m.TPS = 300
m.FPS = 0
m.AddUISystem(
(&window.Window{}).
With(&plot.Image{
Observer: &MatrixObserver{},
Colors: colorgrad.Inferno(),
}))

m.AddSystem(&system.FixedTermination{
Steps: 100,
})

m.Run()
}

// Example observer, reporting a matrix with z = sin(0.1*i) + sin(0.2*j).
type MatrixObserver struct {
cols int
Expand Down
48 changes: 48 additions & 0 deletions plot/inspector_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package plot_test

import (
"testing"

"github.com/mlange-42/arche-model/model"
"github.com/mlange-42/arche-model/resource"
"github.com/mlange-42/arche-model/system"
Expand Down Expand Up @@ -43,3 +45,49 @@ func ExampleInspector() {

// Output:
}

func TestInspector(t *testing.T) {
m := model.New()
m.TPS = 300

posID := ecs.ComponentID[Position](&m.World)
velID := ecs.ComponentID[Velocity](&m.World)
entity := m.World.NewEntity(posID, velID)

ecs.AddResource(&m.World, &resource.SelectedEntity{Selected: entity})

m.AddUISystem((&window.Window{}).
With(&plot.Inspector{
HideNames: true,
}))

m.AddSystem(&system.FixedTermination{
Steps: 100,
})

m.Run()
}

func TestInspector_DeadEntity(t *testing.T) {
m := model.New()
m.TPS = 300

posID := ecs.ComponentID[Position](&m.World)
velID := ecs.ComponentID[Velocity](&m.World)
entity := m.World.NewEntity(posID, velID)

ecs.AddResource(&m.World, &resource.SelectedEntity{Selected: entity})

m.AddUISystem((&window.Window{}).
With(&plot.Inspector{
HideNames: true,
}))

m.AddSystem(&system.FixedTermination{
Steps: 100,
})

m.World.RemoveEntity(entity)

m.Run()
}
49 changes: 49 additions & 0 deletions plot/lines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package plot_test

import (
"math/rand"
"testing"

"github.com/mlange-42/arche-model/model"
"github.com/mlange-42/arche-model/system"
"github.com/mlange-42/arche-pixel/plot"
"github.com/mlange-42/arche-pixel/window"
"github.com/mlange-42/arche/ecs"
"github.com/stretchr/testify/assert"
)

func ExampleLines() {
Expand Down Expand Up @@ -43,6 +45,53 @@ func ExampleLines() {
// Output:
}

func TestLines(t *testing.T) {
m := model.New()
m.TPS = 300
m.AddUISystem((&window.Window{}).
With(&plot.Lines{
Observer: &TableObserver{},
YLim: [2]float64{0.5, 0.6},
}))

m.AddSystem(&system.FixedTermination{
Steps: 100,
})

m.Run()
}

func TestLines_PanicX(t *testing.T) {
m := model.New()
m.AddUISystem((&window.Window{}).
With(&plot.Lines{
Observer: &TableObserver{},
X: "U",
}))

m.AddSystem(&system.FixedTermination{
Steps: 100,
})

assert.Panics(t, m.Run)
}

func TestLines_PanicY(t *testing.T) {
m := model.New()
m.AddUISystem((&window.Window{}).
With(&plot.Lines{
Observer: &TableObserver{},
X: "X",
Y: []string{"A", "B", "U"},
}))

m.AddSystem(&system.FixedTermination{
Steps: 100,
})

assert.Panics(t, m.Run)
}

// TableObserver to generate random time series.
type TableObserver struct {
data [][]float64
Expand Down
91 changes: 91 additions & 0 deletions plot/rgb_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package plot_test

import (
"math"
"testing"

"github.com/mlange-42/arche-model/model"
"github.com/mlange-42/arche-model/observer"
"github.com/mlange-42/arche-model/system"
"github.com/mlange-42/arche-pixel/plot"
"github.com/mlange-42/arche-pixel/window"
"github.com/mlange-42/arche/ecs"
"github.com/stretchr/testify/assert"
)

func ExampleImageRGB() {
Expand Down Expand Up @@ -48,6 +50,95 @@ func ExampleImageRGB() {
// Output:
}

func TestImageRGB(t *testing.T) {
m := model.New()
m.TPS = 300
m.AddUISystem((&window.Window{}).
With(&plot.ImageRGB{
Observer: observer.MatrixToLayers(
&CallbackMatrixObserver{Callback: func(i, j int) float64 { return float64(i) / 240 }},
&CallbackMatrixObserver{Callback: func(i, j int) float64 { return math.Sin(0.1 * float64(i)) }},
&CallbackMatrixObserver{Callback: func(i, j int) float64 { return float64(j) / 160 }},
),
}))
m.AddSystem(&system.FixedTermination{
Steps: 100,
})
m.Run()
}

func TestImageRGB_PanicMin(t *testing.T) {
m := model.New()
m.TPS = 300
m.AddUISystem((&window.Window{}).
With(&plot.ImageRGB{
Observer: observer.MatrixToLayers(
&CallbackMatrixObserver{Callback: func(i, j int) float64 { return float64(i) / 240 }},
&CallbackMatrixObserver{Callback: func(i, j int) float64 { return math.Sin(0.1 * float64(i)) }},
&CallbackMatrixObserver{Callback: func(i, j int) float64 { return float64(j) / 160 }},
),
Min: []float64{0, 0},
}))
m.AddSystem(&system.FixedTermination{
Steps: 100,
})
assert.Panics(t, m.Run)
}

func TestImageRGB_PanicMax(t *testing.T) {
m := model.New()
m.TPS = 300
m.AddUISystem((&window.Window{}).
With(&plot.ImageRGB{
Observer: observer.MatrixToLayers(
&CallbackMatrixObserver{Callback: func(i, j int) float64 { return float64(i) / 240 }},
&CallbackMatrixObserver{Callback: func(i, j int) float64 { return math.Sin(0.1 * float64(i)) }},
&CallbackMatrixObserver{Callback: func(i, j int) float64 { return float64(j) / 160 }},
),
Max: []float64{1, 1},
}))
m.AddSystem(&system.FixedTermination{
Steps: 100,
})
assert.Panics(t, m.Run)
}

func TestImageRGB_PanicLayerCount(t *testing.T) {
m := model.New()
m.TPS = 300
m.AddUISystem((&window.Window{}).
With(&plot.ImageRGB{
Observer: observer.MatrixToLayers(
&CallbackMatrixObserver{Callback: func(i, j int) float64 { return float64(i) / 240 }},
&CallbackMatrixObserver{Callback: func(i, j int) float64 { return math.Sin(0.1 * float64(i)) }},
&CallbackMatrixObserver{Callback: func(i, j int) float64 { return float64(j) / 160 }},
),
Layers: []int{2, 1, 2, 0},
}))
m.AddSystem(&system.FixedTermination{
Steps: 100,
})
assert.Panics(t, m.Run)
}

func TestImageRGB_PanicLayerIndex(t *testing.T) {
m := model.New()
m.TPS = 300
m.AddUISystem((&window.Window{}).
With(&plot.ImageRGB{
Observer: observer.MatrixToLayers(
&CallbackMatrixObserver{Callback: func(i, j int) float64 { return float64(i) / 240 }},
&CallbackMatrixObserver{Callback: func(i, j int) float64 { return math.Sin(0.1 * float64(i)) }},
&CallbackMatrixObserver{Callback: func(i, j int) float64 { return float64(j) / 160 }},
),
Layers: []int{0, 1, 3},
}))
m.AddSystem(&system.FixedTermination{
Steps: 100,
})
assert.Panics(t, m.Run)
}

// Example observer, reporting a matrix filled with a callback(i, j).
type CallbackMatrixObserver struct {
Callback func(i, j int) float64
Expand Down

0 comments on commit 3311d41

Please sign in to comment.