Debian Codegraph @

dstream/gen/comparisons.tmpl

raw
1 package dstream
2
3 import (
4 "fmt"
5 "os"
6 "time"
7 )
8
9 func namesEqual(x, y []string) string {
10
11 na := make([]map[string]bool, 2)
12 for j, m := range [][]string{x, y} {
13 na[j] = make(map[string]bool)
14 for _, v := range m {
15 na[j][v] = true
16 }
17 }
18
19 for v, _ := range na[0] {
20 if !na[1][v] {
21 return fmt.Sprintf("x variable '%s' not found in y.", v)
22 }
23 }
24
25 for v, _ := range na[1] {
26 if !na[0][v] {
27 return fmt.Sprintf("y variable '%s' not found in x.", v)
28 }
29 }
30
31 return ""
32 }
33
34
35 // EqualReport compares two Dstream values. If they are not equal,
36 // further information is written to the standard error stream. Equality
37 // here implies that the data values, types, order, and chunk
38 // boundaries are all identical.
39 func EqualReport(x, y Dstream, report bool) bool {
40
41 x.Reset()
42 y.Reset()
43
44 if x.NumVar() != y.NumVar() {
45 if report {
46 msg := fmt.Sprintf("Number of variables differ:\nx: %d\ny: %d\n",
47 x.NumVar(), y.NumVar())
48 os.Stderr.WriteString(msg)
49 }
50 return false
51 }
52
53 // Check variable names
54 if msg := namesEqual(x.Names(), y.Names()); msg != "" {
55 panic(msg)
56 }
57
58 for chunk := 0; x.Next(); chunk++ {
59
60 if !y.Next() {
61 if report {
62 msg := fmt.Sprintf("unequal numbers of chunks (y has fewer chunks than x)\n")
63 print(msg)
64 }
65 return false
66 }
67
68 for _, na := range x.Names() {
69 switch v := x.Get(na).(type) {
70 {{ range . }}
71 case []{{ .Type }}:
72 u, ok := y.Get(na).([]{{ .Type }})
73 if !ok || !aequal{{ .Utype }}(v, u) {
74 if report {
75 fmt.Printf("Chunk %d, %s\n", chunk, na)
76 fmt.Printf(" Unequal floats:\n (1) %v\n (2) %v\n", v, u)
77 }
78 return false
79 }
80 {{ end }}
81 default:
82 if report {
83 print("mismatched types")
84 }
85 return false
86 }
87 }
88 }
89
90 if y.Next() {
91 if report {
92 msg := fmt.Sprintf("unequal numbers of chunks (x has fewer chunks than y)\n")
93 print(msg)
94 }
95 return false
96 }
97
98 return true
99 }
100
101
102 {{ range . }}
103 func aequal{{ .Utype}}(x, y []{{ .Type }}) bool {
104 if len(x) != len(y) {
105 return false
106 }
107 for i, v := range x {
108 if v != y[i] {
109 return false
110 }
111 }
112 return true
113 }
114 {{ end }}