Get the average consolidation working.
[otsdb-go.git] / ots_timeseries_test.go
diff --git a/ots_timeseries_test.go b/ots_timeseries_test.go
new file mode 100644 (file)
index 0000000..8df850e
--- /dev/null
@@ -0,0 +1,70 @@
+package otsdb
+
+import (
+  "math"
+  "testing"
+)
+
+type consolidatePointAverageTest struct {
+  tsStart float64
+  tsEnd   float64
+  rate    float64
+}
+
+var consolidatePointAverageTestData = []OTS_DataPoint {
+  {  0.0,  0.0},
+  { 10.0,  2.0},
+  { 20.0,  4.0},
+  { 30.0,  8.0},
+  { 40.0, 16.0},
+  { 50.0, 32.0},
+  { 60.0, 64.0},
+  { 70.0, 96.0},
+  { 80.0, 96.0},
+  { 90.0,  0.0},
+  {100.0,  0.0},
+  {110.0,  0.0},
+  {120.0,  0.0},
+}
+
+var consolidatePointAverageTests = []consolidatePointAverageTest {
+  /* Timespan borders align with datapoints. This is the easiest case. */
+  consolidatePointAverageTest{40.0,  60.0, 48.0},
+  consolidatePointAverageTest{40.0,  50.0, 32.0},
+  /* Timespan borders between datapoints. */
+  consolidatePointAverageTest{35.0,  45.0, 24.0},
+  consolidatePointAverageTest{ 7.0,  27.0,  5.1},
+  consolidatePointAverageTest{17.0,  42.0, 12.64},
+  /* No datapoints within timespan. */
+  consolidatePointAverageTest{23.0,  28.0,  8.0},
+  /* Beginning before first datapoint */
+  consolidatePointAverageTest{-8.0,  24.0,  2.875},
+  /* End after last datapoint */
+  consolidatePointAverageTest{60.0, 180.0, 32.0},
+  /* Start and end inversed */
+  consolidatePointAverageTest{27.0,   7.0,  5.1},
+}
+
+func FloatsDiffer (a, b float64) bool {
+  if math.Fabs (a - b) > 1.0e-6 {
+    return true
+  }
+  return false
+}
+
+func TestConsolidatePointAverage (t *testing.T) {
+  obj := new (OTS_TimeSeries)
+  obj.DataPoints = consolidatePointAverageTestData
+
+  for i := 0; i < len (consolidatePointAverageTests); i++ {
+    testCase := consolidatePointAverageTests[i]
+
+    dp := obj.ConsolidatePointAverage (testCase.tsStart, testCase.tsEnd);
+    if FloatsDiffer (dp.Rate, testCase.rate) {
+      t.Errorf ("ConsolidatePointAverage (%g, %g) failed: Expected %g, got %g",
+          testCase.tsStart, testCase.tsEnd, testCase.rate, dp.Rate);
+    }
+  }
+}
+
+/* vim: set syntax=go sw=2 sts=2 et : */