|
| 1 | +package bloom |
| 2 | + |
| 3 | +import "testing" |
| 4 | + |
| 5 | +func TestBloomFilter(t *testing.T) { |
| 6 | + bf := GetBloomFilter() |
| 7 | + test_data := [][]rune{ |
| 8 | + []rune("test1"), |
| 9 | + []rune("test2"), |
| 10 | + []rune("test3"), |
| 11 | + } |
| 12 | + for _, data := range test_data { |
| 13 | + bf.Add(data) |
| 14 | + if !bf.Exists(data) { |
| 15 | + t.Errorf("Expected %s to be in the bloom filter", string(data)) |
| 16 | + } |
| 17 | + if bf.Exists([]rune("not_in_filter")) { |
| 18 | + t.Error("Expected 'not_in_filter' to not be in the bloom filter") |
| 19 | + } |
| 20 | + if bf.Exists([]rune("1test")) { |
| 21 | + t.Error("Expected '1test' to not be in the bloom filter") |
| 22 | + } |
| 23 | + if bf.Exists([]rune("2test")) { |
| 24 | + t.Error("Expected '2test' to not be in the bloom filter") |
| 25 | + } |
| 26 | + if bf.Exists([]rune("3test")) { |
| 27 | + t.Error("Expected '3test' to not be in the bloom filter") |
| 28 | + } |
| 29 | + } |
| 30 | + for _, data := range test_data { |
| 31 | + bf.Remove(data) |
| 32 | + if bf.Exists(data) { |
| 33 | + t.Errorf("Expected %s to be removed from the bloom filter", string(data)) |
| 34 | + } |
| 35 | + } |
| 36 | + if bf.Exists([]rune("test1")) || bf.Exists([]rune("test2")) || bf.Exists([]rune("test3")) { |
| 37 | + t.Error("Expected all test data to be removed from the bloom filter") |
| 38 | + } |
| 39 | + if bf.Exists([]rune("not_in_filter")) { |
| 40 | + t.Error("Expected 'not_in_filter' to not be in the bloom filter after removal") |
| 41 | + } |
| 42 | + if bf.Exists([]rune("1test")) { |
| 43 | + t.Error("Expected '1test' to not be in the bloom filter after removal") |
| 44 | + } |
| 45 | + if bf.Exists([]rune("2test")) { |
| 46 | + t.Error("Expected '2test' to not be in the bloom filter after removal") |
| 47 | + } |
| 48 | + if bf.Exists([]rune("3test")) { |
| 49 | + t.Error("Expected '3test' to not be in the bloom filter after removal") |
| 50 | + } |
| 51 | + t.Log("Bloom filter test passed") |
| 52 | +} |
| 53 | + |
| 54 | +func TestBloomFilterChinese(t *testing.T) { |
| 55 | + bf := GetBloomFilter() |
| 56 | + test_data := [][]rune{ |
| 57 | + []rune("测试1"), |
| 58 | + []rune("测试2"), |
| 59 | + []rune("测试3"), |
| 60 | + } |
| 61 | + for _, data := range test_data { |
| 62 | + bf.Add(data) |
| 63 | + if !bf.Exists(data) { |
| 64 | + t.Errorf("Expected %s to be in the bloom filter", string(data)) |
| 65 | + } |
| 66 | + if bf.Exists([]rune("不在过滤器中")) { |
| 67 | + t.Error("Expected '不在过滤器中' to not be in the bloom filter") |
| 68 | + } |
| 69 | + } |
| 70 | + for _, data := range test_data { |
| 71 | + bf.Remove(data) |
| 72 | + if bf.Exists(data) { |
| 73 | + t.Errorf("Expected %s to be removed from the bloom filter", string(data)) |
| 74 | + } |
| 75 | + } |
| 76 | + if bf.Exists([]rune("测试1")) || bf.Exists([]rune("测试2")) || bf.Exists([]rune("测试3")) { |
| 77 | + t.Error("Expected all test data to be removed from the bloom filter") |
| 78 | + } |
| 79 | + if bf.Exists([]rune("不在过滤器中")) { |
| 80 | + t.Error("Expected '不在过滤器中' to not be in the bloom filter after removal") |
| 81 | + } |
| 82 | + t.Log("Bloom filter Chinese test passed") |
| 83 | +} |
0 commit comments