From 6c9bcbbba32bc5e09f81f419f51a3a50256be979 Mon Sep 17 00:00:00 2001 From: Philipp Matthes Date: Mon, 29 Apr 2024 09:12:32 +0200 Subject: [PATCH] Add history test --- histories/history_test.go | 145 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 histories/history_test.go diff --git a/histories/history_test.go b/histories/history_test.go new file mode 100644 index 0000000..c2a1362 --- /dev/null +++ b/histories/history_test.go @@ -0,0 +1,145 @@ +package histories + +import ( + "reflect" + "testing" + "time" +) + +func TestFlattenHistory(t *testing.T) { + exampleCycle := HistoryCycle{ + StartTime: time.Unix(0, 0), + EndTime: time.Unix(10, 0), // Usually cycles are longer, but suffices for testing + Phases: []HistoryPhaseEvent{ + // Green at 0 seconds + { + Time: time.Unix(0, 0), + Color: 3, + }, + // Red at 5 seconds + { + Time: time.Unix(5, 0), + Color: 1, + }, + }, + } + history := History{ + Cycles: []HistoryCycle{ + exampleCycle, + exampleCycle, + }, + } + expectedArr := [][]byte{ + { + 3, 3, 3, 3, 3, + 1, 1, 1, 1, 1, + }, + { + 3, 3, 3, 3, 3, + 1, 1, 1, 1, 1, + }, + } + actualArr := history.Flatten() + if !reflect.DeepEqual(expectedArr, actualArr) { + t.Errorf("unexpected result") + } +} + +func TestErroneousCyclesArePruned(t *testing.T) { + validCycle := HistoryCycle{ + StartTime: time.Unix(0, 0), + EndTime: time.Unix(10, 0), // Usually cycles are longer, but suffices for testing + Phases: []HistoryPhaseEvent{ + // Green at 0 seconds + { + Time: time.Unix(0, 0), + Color: 3, + }, + // Red at 5 seconds + { + Time: time.Unix(5, 0), + Color: 1, + }, + }, + } + startTimeBeforeEndTime := HistoryCycle{ + StartTime: time.Unix(10, 0), + EndTime: time.Unix(0, 0), + Phases: []HistoryPhaseEvent{ + // Green at 0 seconds + { + Time: time.Unix(0, 0), + Color: 3, + }, + // Red at 5 seconds + { + Time: time.Unix(5, 0), + Color: 1, + }, + }, + } + tooShort := HistoryCycle{ + StartTime: time.Unix(0, 0), + EndTime: time.Unix(5, 0), + Phases: []HistoryPhaseEvent{ + // Green at 0 seconds + { + Time: time.Unix(0, 0), + Color: 3, + }, + // Red at 3 seconds + { + Time: time.Unix(3, 0), + Color: 1, + }, + }, + } + tooLong := HistoryCycle{ + StartTime: time.Unix(0, 0), + EndTime: time.Unix(400, 0), + Phases: []HistoryPhaseEvent{ + // Green at 0 seconds + { + Time: time.Unix(0, 0), + Color: 3, + }, + // Red at 100 seconds + { + Time: time.Unix(100, 0), + Color: 1, + }, + }, + } + empty := HistoryCycle{ + StartTime: time.Unix(0, 0), + EndTime: time.Unix(90, 0), + } + history := History{ + Cycles: []HistoryCycle{ + validCycle, + startTimeBeforeEndTime, + tooShort, + tooLong, + empty, + }, + } + expectedArr := [][]byte{ + { + 3, 3, 3, 3, 3, + 1, 1, 1, 1, 1, + }, + } + actualArr := history.Flatten() + // Using DeepEqual is fine for testing + if !reflect.DeepEqual(expectedArr, actualArr) { + t.Errorf("unexpected result") + } +} + +func TestEmptyHistoryFlattening(t *testing.T) { + h := History{} + // Using DeepEqual is fine for testing + if !reflect.DeepEqual(h.Flatten(), [][]byte{}) { + t.Errorf("empty history flatten should result in empty 2d slice") + } +}