Commit 1131fb5d authored by Aral Balkan's avatar Aral Balkan
Browse files

Added a test for actually generated codes. Running regular tests 1,000 times...

Added a test for actually generated codes. Running regular tests 1,000 times to get insight into uniqueness limit.
parent ab7a51f4
assert = require 'assert'
fs = require 'fs'
#
# Implementation of hash sieve algorithm that I read
# about at http://www.shamasis.net/2009/09/fast-algorithm-to-find-unique-items-in-javascript-array/
# in CoffeeScript with slightly more literate code :)
#
Array.prototype.unique = ->
hashSieve = {}
arrayOfUniqueValues = [];
for item in @
hashSieve[item] = item
for item of hashSieve
arrayOfUniqueValues.push item
return arrayOfUniqueValues
#
# Test that codes really are unique.
#
a = JSON.parse(fs.readFileSync('codes/1414632363426/tier-100.json'))
b = JSON.parse(fs.readFileSync('codes/1414632363426/tier-1000.json'))
c = a.concat b
c = c.unique()
assert.equal c.length, a.length + b.length, 'Codes should be unique across both tiers'
console.log 'All codes are unique across the $100 and $1,000 tiers.'
\ No newline at end of file
......@@ -23,19 +23,25 @@ Array.prototype.unique = ->
# Test that codes really are unique.
#
codeGenerator = new CodeGenerator()
codes = codeGenerator.generateUniqueCodes(1000)
codes = codes.unique()
assert.equal codes.length, 1000, 'There should be 1,000 unique codes.'
numCodes = 10000
numTimes = 1000
while numTimes
process.stdout.write 'Running test #' + numTimes + ' \r'
codes = codeGenerator.generateUniqueCodes(numCodes)
codes = codes.unique()
assert.equal codes.length, numCodes, 'There should be ' + numCodes + ' unique codes.'
numTimes--
#
# And that our hash sieve is working :)
#
codes[0] = 'Hello'
codes[1] = 'Hello'
codes = codes.unique()
testArray =['hello', 'there', 'hello', 'again']
testArray = testArray.unique()
assert.equal codes.length, 999, 'There should be 999 unique codes.'
assert.equal testArray.length, 3, 'There should be 3 items.'
assert.equal testArray[2], 'again', 'Third item should be “again”'
console.log 'TESTS OK.'
\ No newline at end of file
console.log '\nTESTS OK.'
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment