File: dropna_test.go

package info (click to toggle)
golang-github-kshedden-dstream 0.0~git20190512.c4c4106-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 596 kB
  • sloc: makefile: 30
file content (122 lines) | stat: -rw-r--r-- 2,248 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package dstream

import (
	"fmt"
	"math"
	"testing"
)

func datam1() (Dstream, Dstream) {
	x1 := []interface{}{
		[]float64{0, 1, 1},
		[]float64{0, math.NaN(), 1, 0},
	}
	x2 := []interface{}{
		[]float64{1, 1, math.NaN()},
		[]float64{1, 1, 1, 1},
	}
	x3 := []interface{}{
		[]float64{4, 1, -1},
		[]float64{3, 5, -5, 3},
	}
	x4 := []interface{}{
		[]string{"a", "b", "c"},
		[]string{"d", "e", "f", "g"},
	}
	dat := [][]interface{}{x1, x2, x3, x4}
	na := []string{"x1", "x2", "x3", "x4"}
	da := NewFromArrays(dat, na)

	x1 = []interface{}{
		[]float64{0, 1},
		[]float64{0, 1, 0},
	}
	x2 = []interface{}{
		[]float64{1, 1},
		[]float64{1, 1, 1},
	}
	x3 = []interface{}{
		[]float64{4, 1},
		[]float64{3, -5, 3},
	}
	x4 = []interface{}{
		[]string{"a", "b"},
		[]string{"d", "f", "g"},
	}
	dat = [][]interface{}{x1, x2, x3, x4}
	na = []string{"x1", "x2", "x3", "x4"}
	dm := NewFromArrays(dat, na)

	return da, dm
}

// Check that we can get variables by name and by position.
func checkPosName(da Dstream) (bool, string) {

	da.Reset()
	da.Next()

	// Make sure we can get variables by name and by position
	for k, na := range da.Names() {

		a := da.GetPos(k)
		b := da.Get(na)

		// TODO other types
		switch u := a.(type) {

		case []float64:
			v := b.([]float64)
			if len(u) != len(v) {
				msg := fmt.Sprintf("Variable '%s', position %d:\n", na, k)
				msg += fmt.Sprintf("Unequal lengths: %d != %d\n", len(u), len(v))
				return false, msg
			}
			for i, x := range u {
				if math.IsNaN(x) && math.IsNaN(v[i]) {
					continue
				}
				if x != v[i] {
					msg := fmt.Sprintf("Variable '%s', position %d:\n", na, k)
					return false, msg
				}
			}
		case []string:
			v := b.([]string)
			if len(u) != len(v) {
				msg := fmt.Sprintf("Variable '%s', position %d:\n", na, k)
				msg += fmt.Sprintf("Unequal lengths: %d != %d\n", len(u), len(v))
				return false, msg
			}
			for i, x := range u {
				if x != v[i] {
					return false, ""
				}
			}
		}
	}

	return true, ""
}

func TestDropNA1(t *testing.T) {

	da, de := datam1()
	dm := DropNA(da)

	dx := MemCopy(dm, true)

	if !EqualReport(de, dm, true) {
		t.Fail()
	}

	if !EqualReport(dm, dx, true) {
		t.Fail()
	}

	f, msg := checkPosName(da)
	if !f {
		print(msg)
		t.Fail()
	}
}