This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
filterTable_test.go
110 lines (85 loc) · 2.97 KB
/
filterTable_test.go
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
package contentpubsub
import (
"context"
"testing"
"time"
)
func InitializeRouteStats() *RouteStats {
r := NewRouteStats("indifferent")
p1, _ := NewPredicate("portugal T", 5)
p2, _ := NewPredicate("soccer T/goals R 2 5", 5)
p3, _ := NewPredicate("surf T/bali T/price R 500 850", 5)
r.filters[1] = append(r.filters[1], p1)
r.filters[2] = append(r.filters[2], p2)
r.filters[3] = append(r.filters[3], p3)
return r
}
// TestAddSummarizedFilter validates filter
// summarization in a RouteStat
func TestAddSummarizedFilter(t *testing.T) {
r := InitializeRouteStats()
// Test1: Bigger Predicate encompassed
pTest, _ := NewPredicate("portugal T/soccer T", 5)
r.SimpleAddSummarizedFilter(pTest, nil)
if len(r.filters[2]) == 2 {
t.Fatal("Test1: Wrongly added filter: " + pTest.String())
}
// Test2: Same-size Predicate encompassed
pTest, _ = NewPredicate("soccer T/goals R 2 4", 5)
r.SimpleAddSummarizedFilter(pTest, nil)
if len(r.filters[2]) != 1 {
t.Fatal("Test2: Wrongly added filter: " + pTest.String())
}
// Test3: Same-size Predicate encompassing
pTest, _ = NewPredicate("soccer T/goals R 2 7", 5)
r.SimpleAddSummarizedFilter(pTest, nil)
if len(r.filters[2]) == 2 {
t.Fatal("Test3: Filter not deleted")
} else if r.filters[2][0].attributes["goals"].rangeQuery[1] != 7 {
t.Fatal("Test3: Wrong filter:" + r.filters[2][0].String())
}
// Test4: Smaller Predicate encompassing
pTest, _ = NewPredicate("soccer T", 5)
r.SimpleAddSummarizedFilter(pTest, nil)
if len(r.filters[1]) == 1 {
t.Fatal("Test4: Filter not added: " + pTest.String())
} else if len(r.filters[2]) != 0 {
t.Fatal("Test4: Filter not deleted")
}
// Test5: Upper-limit merge
pTest, _ = NewPredicate("surf T/bali T/price R 700 1000", 5)
r.SimpleAddSummarizedFilter(pTest, nil)
if len(r.filters[3]) != 1 {
t.Fatal("Test5: Filter not deleted")
} else if r.filters[3][0].attributes["price"].rangeQuery[1] != 1000 {
t.Fatal("Test5: Wrong merge:" + r.filters[3][0].String())
}
// Test6: Lower limit merge
pTest, _ = NewPredicate("surf T/bali T/price R 400 600", 5)
r.SimpleAddSummarizedFilter(pTest, nil)
if len(r.filters[3]) == 2 {
t.Fatal("Test6: Filter not deleted")
} else if r.filters[3][0].attributes["price"].rangeQuery[0] != 400 {
t.Fatal("Test6: Wrong merge:" + r.filters[3][0].String())
}
// Test7: Exclusive predicate
pTest, _ = NewPredicate("Tesla T/stock R 10000 15000", 5)
r.SimpleAddSummarizedFilter(pTest, nil)
if len(r.filters[2]) != 1 {
t.Fatal("Test7: Filter not added: " + pTest.String())
}
}
// TestNewFilterTable validates the initialization of a filterTable
func TestNewFilterTable(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Second)
defer cancel()
dhts := setupDHTS(t, ctx, 5)
connect(t, ctx, dhts[0], dhts[1])
connect(t, ctx, dhts[0], dhts[2])
connect(t, ctx, dhts[0], dhts[3])
connect(t, ctx, dhts[0], dhts[4])
ft := NewFilterTable(dhts[0], false)
if len(ft.routes) != 4 {
t.Fatal("Error creating filterTable")
}
}