|
| 1 | +import { copycat } from '.' |
| 2 | +import { randomUUID } from 'crypto' |
| 3 | + |
| 4 | +test('should be able to generate 5 unique numbers with low possibility space', () => { |
| 5 | + const collisionsStore = new Set() |
| 6 | + let numberOfCollisions = 0 |
| 7 | + const method = (seed): number => { |
| 8 | + return copycat.int(seed, { min: 0, max: 5 }) |
| 9 | + } |
| 10 | + const store = new Set<ReturnType<typeof method>>() |
| 11 | + const results: Array<number> = [] |
| 12 | + for (let i = 0; i <= 5; i++) { |
| 13 | + const result = copycat.unique(i, method, store, { |
| 14 | + attempts: 100, |
| 15 | + }) |
| 16 | + results.push(result) |
| 17 | + if (collisionsStore.has(result)) { |
| 18 | + numberOfCollisions++ |
| 19 | + } else { |
| 20 | + collisionsStore.add(result) |
| 21 | + } |
| 22 | + } |
| 23 | + results.sort() |
| 24 | + // Without unique would be [1,1,1,2,2,4] |
| 25 | + expect(results).toEqual([0, 1, 2, 3, 4, 5]) |
| 26 | + expect(numberOfCollisions).toBe(0) |
| 27 | +}) |
| 28 | + |
| 29 | +test('should be able to generate 5 unique numbers with low possibility space based on random seed', () => { |
| 30 | + const collisionsStore = new Set() |
| 31 | + let numberOfCollisions = 0 |
| 32 | + const store = new Set<ReturnType<typeof method>>() |
| 33 | + const method = (seed): number => { |
| 34 | + return copycat.int(seed, { min: 0, max: 5 }) |
| 35 | + } |
| 36 | + const results: Array<number> = [] |
| 37 | + for (let i = 0; i <= 5; i++) { |
| 38 | + const result = copycat.unique(randomUUID(), method, store, { |
| 39 | + attempts: 100, |
| 40 | + }) |
| 41 | + results.push(result) |
| 42 | + if (collisionsStore.has(result)) { |
| 43 | + numberOfCollisions++ |
| 44 | + } else { |
| 45 | + collisionsStore.add(result) |
| 46 | + } |
| 47 | + } |
| 48 | + results.sort() |
| 49 | + expect(results).toEqual([0, 1, 2, 3, 4, 5]) |
| 50 | + expect(numberOfCollisions).toBe(0) |
| 51 | +}) |
| 52 | + |
| 53 | +test('should work with scramble method and not alter method input value', () => { |
| 54 | + const collisionsStore = new Set() |
| 55 | + let numberOfCollisions = 0 |
| 56 | + const store = new Set<ReturnType<typeof method>>() |
| 57 | + const method = (seed): string => { |
| 58 | + return copycat.scramble(seed) |
| 59 | + } |
| 60 | + const results: Array<string> = [] |
| 61 | + const textInputs = [ |
| 62 | + 'hello world', |
| 63 | + 'hello world', |
| 64 | + 'this is another test', |
| 65 | + 'never gonna give you up', |
| 66 | + 'never gonna let you down', |
| 67 | + 'never gonna run around and desert you', |
| 68 | + ] |
| 69 | + for (let i = 0; i <= 5; i++) { |
| 70 | + const result = copycat.unique(textInputs[i], method, store, { |
| 71 | + attempts: 100, |
| 72 | + }) |
| 73 | + results.push(result) |
| 74 | + if (collisionsStore.has(result)) { |
| 75 | + numberOfCollisions++ |
| 76 | + } else { |
| 77 | + collisionsStore.add(result) |
| 78 | + } |
| 79 | + } |
| 80 | + // Without unique the two first results would be the same |
| 81 | + expect(results).toEqual([ |
| 82 | + 'zmnmq ngcsx', |
| 83 | + // Since we are using the same input, we should keep the same "shape" |
| 84 | + 'zvzkv uviju', |
| 85 | + 'wnzx dd tqjjomm jmnz', |
| 86 | + 'ugwfa zugvm xhky hbn fo', |
| 87 | + 'bxhyr wojeg cvi kcu untf', |
| 88 | + 'daylm ztnpp enf clrzkb toe bjhgzu yfu', |
| 89 | + ]) |
| 90 | + expect(numberOfCollisions).toBe(0) |
| 91 | +}) |
| 92 | + |
| 93 | +test('should call attempsReached when no more attempts are available', () => { |
| 94 | + const store = new Set<ReturnType<typeof method>>() |
| 95 | + const method = (seed): number => { |
| 96 | + return copycat.int(seed, { min: 0, max: 5 }) |
| 97 | + } |
| 98 | + let attemptReachedCalled = false |
| 99 | + for (let i = 0; i <= 5; i++) { |
| 100 | + copycat.unique(i, method, store, { |
| 101 | + attempts: 1, |
| 102 | + attemptsReached: () => { |
| 103 | + attemptReachedCalled = true |
| 104 | + }, |
| 105 | + }) |
| 106 | + } |
| 107 | + expect(attemptReachedCalled).toBe(true) |
| 108 | +}) |
| 109 | + |
| 110 | +test('should restore the same hashKey when done', () => { |
| 111 | + const store = new Set<ReturnType<typeof method>>() |
| 112 | + const originalResult = copycat.int(1) |
| 113 | + const method = (seed): number => { |
| 114 | + return copycat.int(seed, { min: 0, max: 5 }) |
| 115 | + } |
| 116 | + for (let i = 0; i <= 5; i++) { |
| 117 | + copycat.unique(i, method, store, { |
| 118 | + attempts: 100, |
| 119 | + }) |
| 120 | + } |
| 121 | + // The hashKey should be restored to its original value |
| 122 | + // so that the next call to copycat.int(1) returns the same result |
| 123 | + // as if copycat.unique was never called. |
| 124 | + expect(copycat.int(1)).toBe(originalResult) |
| 125 | +}) |
| 126 | + |
| 127 | +test('should rethrow too much attempts error', () => { |
| 128 | + const store = new Set<ReturnType<typeof method>>() |
| 129 | + const method = (seed): number => { |
| 130 | + return copycat.int(seed, { min: 0, max: 5 }) |
| 131 | + } |
| 132 | + // Ensure the store is already full |
| 133 | + store.add(0) |
| 134 | + store.add(1) |
| 135 | + store.add(2) |
| 136 | + store.add(3) |
| 137 | + store.add(4) |
| 138 | + store.add(5) |
| 139 | + expect(() => |
| 140 | + copycat.unique(1, method, store, { |
| 141 | + attempts: 1, |
| 142 | + attemptsReached: () => { |
| 143 | + throw new Error('Too much attempts') |
| 144 | + }, |
| 145 | + }) |
| 146 | + ).toThrowError('Too much attempts') |
| 147 | +}) |
| 148 | + |
| 149 | +test('should not alter hash key if method throw an error', () => { |
| 150 | + const store = new Set<ReturnType<typeof method>>() |
| 151 | + const originalResult = copycat.int(1) |
| 152 | + const method = (): number => { |
| 153 | + throw new Error('Method error') |
| 154 | + } |
| 155 | + // Ensure the store is already full |
| 156 | + store.add(0) |
| 157 | + store.add(1) |
| 158 | + store.add(2) |
| 159 | + store.add(3) |
| 160 | + store.add(4) |
| 161 | + store.add(5) |
| 162 | + expect(() => |
| 163 | + copycat.unique(1, method, store, { |
| 164 | + attempts: 1, |
| 165 | + attemptsReached: () => { |
| 166 | + throw new Error('Too much attempts') |
| 167 | + }, |
| 168 | + }) |
| 169 | + ).toThrowError('Method error') |
| 170 | + // The hashKey should be restored to its original value |
| 171 | + // so that the next call to copycat.int(1) returns the same result |
| 172 | + // as if copycat.unique was never called. |
| 173 | + expect(copycat.int(1)).toBe(originalResult) |
| 174 | +}) |
| 175 | + |
| 176 | +test('should restore the same hashKey even if throwing error after too much attempts', () => { |
| 177 | + const store = new Set<ReturnType<typeof method>>() |
| 178 | + const originalResult = copycat.int(1) |
| 179 | + const method = (seed): number => { |
| 180 | + return copycat.int(seed, { min: 0, max: 5 }) |
| 181 | + } |
| 182 | + |
| 183 | + // Ensure the store is already full |
| 184 | + store.add(0) |
| 185 | + store.add(1) |
| 186 | + store.add(2) |
| 187 | + store.add(3) |
| 188 | + store.add(4) |
| 189 | + store.add(5) |
| 190 | + expect(() => |
| 191 | + copycat.unique(1, method, store, { |
| 192 | + attempts: 1, |
| 193 | + attemptsReached: () => { |
| 194 | + throw new Error('Too much attempts') |
| 195 | + }, |
| 196 | + }) |
| 197 | + ).toThrowError('Too much attempts') |
| 198 | + // The hashKey should be restored to its original value |
| 199 | + // so that the next call to copycat.int(1) returns the same result |
| 200 | + // as if copycat.unique was never called. |
| 201 | + expect(copycat.int(1)).toBe(originalResult) |
| 202 | +}) |
| 203 | + |
| 204 | +test('should be able to generate 8999 unique fictional french phone numbers', () => { |
| 205 | + const collisionsStore = new Set() |
| 206 | + let numberOfCollisions = 0 |
| 207 | + const method = (seed): string => { |
| 208 | + return copycat.phoneNumber(seed, { |
| 209 | + prefixes: ['+3319900'], |
| 210 | + min: 1000, |
| 211 | + max: 9999, |
| 212 | + }) |
| 213 | + } |
| 214 | + const store = new Set<ReturnType<typeof method>>() |
| 215 | + const results: Array<string> = [] |
| 216 | + for (let i = 0; i <= 8999; i++) { |
| 217 | + const result = copycat.unique(i, method, store, { |
| 218 | + // Bellow 2500 attempts, the test fails because of collisions |
| 219 | + attempts: 2500, |
| 220 | + }) |
| 221 | + results.push(result) |
| 222 | + if (collisionsStore.has(result)) { |
| 223 | + numberOfCollisions++ |
| 224 | + } else { |
| 225 | + collisionsStore.add(result) |
| 226 | + } |
| 227 | + } |
| 228 | + expect(numberOfCollisions).toBe(0) |
| 229 | +}) |
| 230 | + |
| 231 | +test('should be able to generate 8999 unique fictional french phone numbers with uuid', () => { |
| 232 | + const collisionsStore = new Set() |
| 233 | + let numberOfCollisions = 0 |
| 234 | + const method = (seed): string => { |
| 235 | + return copycat.phoneNumber(seed, { |
| 236 | + prefixes: ['+3319900'], |
| 237 | + min: 1000, |
| 238 | + max: 9999, |
| 239 | + }) |
| 240 | + } |
| 241 | + const store = new Set<ReturnType<typeof method>>() |
| 242 | + const results: Array<string> = [] |
| 243 | + for (let i = 0; i <= 8999; i++) { |
| 244 | + const result = copycat.unique(randomUUID(), method, store, { |
| 245 | + // Bellow 100k with random uuid there is still a 1/1000 chance of collision |
| 246 | + attempts: 100000, |
| 247 | + }) |
| 248 | + results.push(result) |
| 249 | + if (collisionsStore.has(result)) { |
| 250 | + numberOfCollisions++ |
| 251 | + } else { |
| 252 | + collisionsStore.add(result) |
| 253 | + } |
| 254 | + } |
| 255 | + expect(numberOfCollisions).toBe(0) |
| 256 | +}) |
0 commit comments