1// Copyright 2019 The Prometheus Authors 2// Licensed under the Apache License, Version 2.0 (the "License"); 3// you may not use this file except in compliance with the License. 4// You may obtain a copy of the License at 5// 6// http://www.apache.org/licenses/LICENSE-2.0 7// 8// Unless required by applicable law or agreed to in writing, software 9// distributed under the License is distributed on an "AS IS" BASIS, 10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11// See the License for the specific language governing permissions and 12// limitations under the License. 13 14package model 15 16import ( 17 "sort" 18 "testing" 19) 20 21func TestFingerprintFromString(t *testing.T) { 22 fs := "4294967295" 23 24 f, err := FingerprintFromString(fs) 25 26 if err != nil { 27 t.Errorf("unexpected error while getting Fingerprint from string: %s", err.Error()) 28 } 29 30 expected := Fingerprint(285960729237) 31 32 if expected != f { 33 t.Errorf("expected to get %d, but got %d instead", f, expected) 34 } 35 36 f, err = ParseFingerprint(fs) 37 38 if err != nil { 39 t.Errorf("unexpected error while getting Fingerprint from string: %s", err.Error()) 40 } 41 42 if expected != f { 43 t.Errorf("expected to get %d, but got %d instead", f, expected) 44 } 45} 46 47func TestFingerprintsSort(t *testing.T) { 48 fingerPrints := Fingerprints{ 49 14695981039346656037, 50 285960729237, 51 0, 52 4294967295, 53 285960729237, 54 18446744073709551615, 55 } 56 57 sort.Sort(fingerPrints) 58 59 expected := Fingerprints{ 60 0, 61 4294967295, 62 285960729237, 63 285960729237, 64 14695981039346656037, 65 18446744073709551615, 66 } 67 68 for i, f := range fingerPrints { 69 if f != expected[i] { 70 t.Errorf("expected Fingerprint %d, but got %d for index %d", expected[i], f, i) 71 } 72 } 73} 74 75func TestFingerprintSet(t *testing.T) { 76 // Testing with two sets of unequal length. 77 f := FingerprintSet{ 78 14695981039346656037: struct{}{}, 79 0: struct{}{}, 80 4294967295: struct{}{}, 81 285960729237: struct{}{}, 82 18446744073709551615: struct{}{}, 83 } 84 85 f2 := FingerprintSet{ 86 285960729237: struct{}{}, 87 } 88 89 if f.Equal(f2) { 90 t.Errorf("expected two FingerPrintSets of unequal length to be unequal") 91 } 92 93 // Testing with two unequal sets of equal length. 94 f = FingerprintSet{ 95 14695981039346656037: struct{}{}, 96 0: struct{}{}, 97 4294967295: struct{}{}, 98 } 99 100 f2 = FingerprintSet{ 101 14695981039346656037: struct{}{}, 102 0: struct{}{}, 103 285960729237: struct{}{}, 104 } 105 106 if f.Equal(f2) { 107 t.Errorf("expected two FingerPrintSets of unequal content to be unequal") 108 } 109 110 // Testing with equal sets of equal length. 111 f = FingerprintSet{ 112 14695981039346656037: struct{}{}, 113 0: struct{}{}, 114 4294967295: struct{}{}, 115 } 116 117 f2 = FingerprintSet{ 118 14695981039346656037: struct{}{}, 119 0: struct{}{}, 120 4294967295: struct{}{}, 121 } 122 123 if !f.Equal(f2) { 124 t.Errorf("expected two FingerPrintSets of equal content to be equal") 125 } 126} 127 128func TestFingerprintIntersection(t *testing.T) { 129 scenarios := []struct { 130 name string 131 input1 FingerprintSet 132 input2 FingerprintSet 133 expected FingerprintSet 134 }{ 135 { 136 name: "two empty sets", 137 input1: FingerprintSet{}, 138 input2: FingerprintSet{}, 139 expected: FingerprintSet{}, 140 }, 141 { 142 name: "one empty set", 143 input1: FingerprintSet{ 144 0: struct{}{}, 145 }, 146 input2: FingerprintSet{}, 147 expected: FingerprintSet{}, 148 }, 149 { 150 name: "two non-empty unequal sets", 151 input1: FingerprintSet{ 152 14695981039346656037: struct{}{}, 153 0: struct{}{}, 154 4294967295: struct{}{}, 155 }, 156 157 input2: FingerprintSet{ 158 14695981039346656037: struct{}{}, 159 0: struct{}{}, 160 4294967295: struct{}{}, 161 }, 162 expected: FingerprintSet{ 163 14695981039346656037: struct{}{}, 164 0: struct{}{}, 165 4294967295: struct{}{}, 166 }, 167 }, 168 { 169 name: "two non-empty equal sets", 170 input1: FingerprintSet{ 171 14695981039346656037: struct{}{}, 172 0: struct{}{}, 173 285960729237: struct{}{}, 174 }, 175 176 input2: FingerprintSet{ 177 14695981039346656037: struct{}{}, 178 0: struct{}{}, 179 4294967295: struct{}{}, 180 }, 181 expected: FingerprintSet{ 182 14695981039346656037: struct{}{}, 183 0: struct{}{}, 184 }, 185 }, 186 { 187 name: "two non-empty equal sets of unequal length", 188 input1: FingerprintSet{ 189 14695981039346656037: struct{}{}, 190 0: struct{}{}, 191 285960729237: struct{}{}, 192 }, 193 194 input2: FingerprintSet{ 195 14695981039346656037: struct{}{}, 196 0: struct{}{}, 197 }, 198 expected: FingerprintSet{ 199 14695981039346656037: struct{}{}, 200 0: struct{}{}, 201 }, 202 }, 203 } 204 205 for _, scenario := range scenarios { 206 s1 := scenario.input1 207 s2 := scenario.input2 208 actual := s1.Intersection(s2) 209 210 if !actual.Equal(scenario.expected) { 211 t.Errorf("expected %v to be equal to %v", actual, scenario.expected) 212 } 213 } 214} 215