Debian Codegraph @

dstream/dropna_test.go

raw symbols SCIP
1 package dstream
2
3 import (
4 "fmt"
5 "math"
6 "testing"
7 )
8
9 func datam1() (Dstream, Dstream) {
10 x1 := []interface{}{
11 []float64{0, 1, 1},
12 []float64{0, math.NaN(), 1, 0},
13 }
14 x2 := []interface{}{
15 []float64{1, 1, math.NaN()},
16 []float64{1, 1, 1, 1},
17 }
18 x3 := []interface{}{
19 []float64{4, 1, -1},
20 []float64{3, 5, -5, 3},
21 }
22 x4 := []interface{}{
23 []string{"a", "b", "c"},
24 []string{"d", "e", "f", "g"},
25 }
26 dat := [][]interface{}{x1, x2, x3, x4}
27 na := []string{"x1", "x2", "x3", "x4"}
28 da := NewFromArrays(dat, na)
29
30 x1 = []interface{}{
31 []float64{0, 1},
32 []float64{0, 1, 0},
33 }
34 x2 = []interface{}{
35 []float64{1, 1},
36 []float64{1, 1, 1},
37 }
38 x3 = []interface{}{
39 []float64{4, 1},
40 []float64{3, -5, 3},
41 }
42 x4 = []interface{}{
43 []string{"a", "b"},
44 []string{"d", "f", "g"},
45 }
46 dat = [][]interface{}{x1, x2, x3, x4}
47 na = []string{"x1", "x2", "x3", "x4"}
48 dm := NewFromArrays(dat, na)
49
50 return da, dm
51 }
52
53 // Check that we can get variables by name and by position.
54 func checkPosName(da Dstream) (bool, string) {
55
56 da.Reset()
57 da.Next()
58
59 // Make sure we can get variables by name and by position
60 for k, na := range da.Names() {
61
62 a := da.GetPos(k)
63 b := da.Get(na)
64
65 // TODO other types
66 switch u := a.(type) {
67
68 case []float64:
69 v := b.([]float64)
70 if len(u) != len(v) {
71 msg := fmt.Sprintf("Variable '%s', position %d:\n", na, k)
72 msg += fmt.Sprintf("Unequal lengths: %d != %d\n", len(u), len(v))
73 return false, msg
74 }
75 for i, x := range u {
76 if math.IsNaN(x) && math.IsNaN(v[i]) {
77 continue
78 }
79 if x != v[i] {
80 msg := fmt.Sprintf("Variable '%s', position %d:\n", na, k)
81 return false, msg
82 }
83 }
84 case []string:
85 v := b.([]string)
86 if len(u) != len(v) {
87 msg := fmt.Sprintf("Variable '%s', position %d:\n", na, k)
88 msg += fmt.Sprintf("Unequal lengths: %d != %d\n", len(u), len(v))
89 return false, msg
90 }
91 for i, x := range u {
92 if x != v[i] {
93 return false, ""
94 }
95 }
96 }
97 }
98
99 return true, ""
100 }
101
102 func TestDropNA1(t *testing.T) {
103
104 da, de := datam1()
105 dm := DropNA(da)
106
107 dx := MemCopy(dm, true)
108
109 if !EqualReport(de, dm, true) {
110 t.Fail()
111 }
112
113 if !EqualReport(dm, dx, true) {
114 t.Fail()
115 }
116
117 f, msg := checkPosName(da)
118 if !f {
119 print(msg)
120 t.Fail()
121 }
122 }