File: formatted_test.go

package info (click to toggle)
golang-github-cloudflare-redoctober 0.0~git20161017.0.78e9720-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 584 kB
  • ctags: 560
  • sloc: sh: 65; makefile: 7
file content (103 lines) | stat: -rw-r--r-- 1,946 bytes parent folder | download | duplicates (3)
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
package msp

import (
	"testing"
)

func TestFormatted(t *testing.T) {
	query1 := Formatted{
		Min: 2,
		Conds: []Condition{
			Name{"Alice", 0}, Name{"Bob", 0}, Name{"Carl", 0},
		},
	}

	query2 := Formatted{
		Min: 3,
		Conds: []Condition{
			Name{"Alice", 0}, Name{"Bob", 0}, Name{"Carl", 0},
		},
	}

	query3 := Formatted{
		Min: 2,
		Conds: []Condition{
			Formatted{
				Min: 1,
				Conds: []Condition{
					Name{"Alice", 0}, Name{"Bob", 0},
				},
			},
			Name{"Carl", 0},
		},
	}

	query4 := Formatted{
		Min: 2,
		Conds: []Condition{
			Formatted{
				Min: 1,
				Conds: []Condition{
					Name{"Alice", 0}, Name{"Carl", 0},
				},
			},
			Name{"Bob", 0},
		},
	}

	db := &Database{
		"Alice": [][]byte{[]byte("blah")},
		"Carl":  [][]byte{[]byte("herp")},
	}

	if query1.Ok(db) != true {
		t.Fatalf("Query #1 was wrong.")
	}

	if query2.Ok(db) != false {
		t.Fatalf("Query #2 was wrong.")
	}

	if query3.Ok(db) != true {
		t.Fatalf("Query #3 was wrong.")
	}

	if query4.Ok(db) != false {
		t.Fatalf("Query #4 was wrong.")
	}

	query1String := "(2, Alice, Bob, Carl)"
	query3String := "(2, (1, Alice, Bob), Carl)"

	if query1.String() != query1String {
		t.Fatalf("Query #1 String was wrong; %v", query1.String())
	}

	if query3.String() != query3String {
		t.Fatalf("Query #3 String was wrong; %v", query3.String())
	}

	decQuery1, err := StringToFormatted(query1String)
	if err != nil || decQuery1.String() != query1String {
		t.Fatalf("Query #1 decoded wrong: %v %v", decQuery1.String(), err)
	}

	decQuery3, err := StringToFormatted(query3String)
	if err != nil || decQuery3.String() != query3String {
		t.Fatalf("Query #3 decoded wrong: %v %v", decQuery3.String(), err)
	}
}

func TestBugs(t *testing.T) {
	bugs := []string{
		"(),)",
		"((2, Alice, Bob), Bob, Carl)",
	}

	for _, bug := range bugs {
		_, err := StringToFormatted(bug)
		if err == nil {
			t.Fatalf("Didn't panic or error on a malformed string: %v", bug)
		}
	}
}