Debian Codegraph @

dstream/convert_test.go

raw symbols SCIP
1 package dstream
2
3 import (
4 "bytes"
5 "testing"
6 )
7
8 func TestConvert1(t *testing.T) {
9
10 x := [][]interface{}{
11 {
12 []float64{0, 0, 0},
13 []float64{1, 1, 1},
14 []float64{2, 2, 3},
15 },
16 {
17 []string{"a", "b", "c"},
18 []string{"d", "e", "f"},
19 []string{"g", "h", "i"},
20 },
21 {
22 []float64{1, 2, 3},
23 []float64{4, 5, 6},
24 []float64{7, 8, 9},
25 },
26 }
27 na := []string{"x1", "x2", "x3"}
28 da := NewFromArrays(x, na)
29
30 z := [][]interface{}{
31 {
32 []int32{0, 0, 0},
33 []int32{1, 1, 1},
34 []int32{2, 2, 3},
35 },
36 {
37 []string{"a", "b", "c"},
38 []string{"d", "e", "f"},
39 []string{"g", "h", "i"},
40 },
41 {
42 []float32{1, 2, 3},
43 []float32{4, 5, 6},
44 []float32{7, 8, 9},
45 },
46 }
47 db := NewFromArrays(z, na)
48
49 // Perform two conversions
50 da = Convert(da, "x1", Int32)
51 da = Convert(da, "x3", Float32)
52
53 if !EqualReport(da, db, true) {
54 t.Fail()
55 }
56 }
57
58 func TestConvert2(t *testing.T) {
59
60 data1 := `id,v1,v2,v3
61 1,2,3,4
62 1,3,4,5
63 2,4,5,6
64 3,5,6,7
65 3,99,99,99
66 3,100,101,102
67 4,200,201,202
68 `
69
70 // Generate a new variable that is 100 times the id variable.
71 times100 := func(v map[string]interface{}, z interface{}) {
72 id := v["id"].([]float64)
73 y := z.([]float64)
74 for i := range id {
75 y[i] = id[i] * 100
76 }
77 }
78
79 b1 := bytes.NewReader([]byte(data1))
80 types := []VarType{
81 {"id", Float64},
82 {"v1", Float64},
83 {"v2", Float64},
84 {"v3", Float64},
85 }
86 d1 := FromCSV(b1).SetTypes(types).HasHeader().Done()
87
88 d1 = Generate(d1, "id100", times100, Float64)
89 d1 = Convert(d1, "id100", Uint64)
90 d1 = DropCols(d1, "id")
91
92 if !d1.Next() {
93 t.Fail()
94 }
95
96 z := d1.Get("id100").([]uint64)
97 y := []uint64{100, 100, 200, 300, 300, 300, 400}
98 for i, u := range z {
99 if u != y[i] {
100 t.Fail()
101 }
102 }
103 }