package dstream import ( "fmt" "os" "time" ) func namesEqual(x, y []string) string { na := make([]map[string]bool, 2) for j, m := range [][]string{x, y} { na[j] = make(map[string]bool) for _, v := range m { na[j][v] = true } } for v, _ := range na[0] { if !na[1][v] { return fmt.Sprintf("x variable '%s' not found in y.", v) } } for v, _ := range na[1] { if !na[0][v] { return fmt.Sprintf("y variable '%s' not found in x.", v) } } return "" } // EqualReport compares two Dstream values. If they are not equal, // further information is written to the standard error stream. Equality // here implies that the data values, types, order, and chunk // boundaries are all identical. func EqualReport(x, y Dstream, report bool) bool { x.Reset() y.Reset() if x.NumVar() != y.NumVar() { if report { msg := fmt.Sprintf("Number of variables differ:\nx: %d\ny: %d\n", x.NumVar(), y.NumVar()) os.Stderr.WriteString(msg) } return false } // Check variable names if msg := namesEqual(x.Names(), y.Names()); msg != "" { panic(msg) } for chunk := 0; x.Next(); chunk++ { if !y.Next() { if report { msg := fmt.Sprintf("unequal numbers of chunks (y has fewer chunks than x)\n") print(msg) } return false } for _, na := range x.Names() { switch v := x.Get(na).(type) { {{ range . }} case []{{ .Type }}: u, ok := y.Get(na).([]{{ .Type }}) if !ok || !aequal{{ .Utype }}(v, u) { if report { fmt.Printf("Chunk %d, %s\n", chunk, na) fmt.Printf(" Unequal floats:\n (1) %v\n (2) %v\n", v, u) } return false } {{ end }} default: if report { print("mismatched types") } return false } } } if y.Next() { if report { msg := fmt.Sprintf("unequal numbers of chunks (x has fewer chunks than y)\n") print(msg) } return false } return true } {{ range . }} func aequal{{ .Utype}}(x, y []{{ .Type }}) bool { if len(x) != len(y) { return false } for i, v := range x { if v != y[i] { return false } } return true } {{ end }}