|
| 1 | +/** |
| 2 | + * Copyright (c) 2014-present, Facebook, Inc. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +///<reference path='../resources/jest.d.ts'/> |
| 9 | + |
| 10 | +import * as jasmineCheck from 'jasmine-check'; |
| 11 | +jasmineCheck.install(); |
| 12 | + |
| 13 | +import { hash } from '../'; |
| 14 | + |
| 15 | +describe('hash', () => { |
| 16 | + |
| 17 | + it('stable hash of well known values', () => { |
| 18 | + expect(hash(true)).toBe(1); |
| 19 | + expect(hash(false)).toBe(0); |
| 20 | + expect(hash(0)).toBe(0); |
| 21 | + expect(hash(null)).toBe(0); |
| 22 | + expect(hash(undefined)).toBe(0); |
| 23 | + expect(hash('a')).toBe(97); |
| 24 | + expect(hash('immutable-js')).toBe(510203252); |
| 25 | + expect(hash(123)).toBe(123); |
| 26 | + }); |
| 27 | + |
| 28 | + it('generates different hashes for decimal values', () => { |
| 29 | + expect(hash(123.456)).toBe(884763256); |
| 30 | + expect(hash(123.4567)).toBe(887769707); |
| 31 | + }); |
| 32 | + |
| 33 | + const genValue = gen.oneOf([gen.string, gen.int]); |
| 34 | + |
| 35 | + check.it('generates unsigned 31-bit integers', [genValue], value => { |
| 36 | + const hashVal = hash(value); |
| 37 | + expect(hashVal % 1).toBe(0); |
| 38 | + expect(hashVal).toBeLessThan(Math.pow(2, 31)); |
| 39 | + }); |
| 40 | + |
| 41 | +}); |
0 commit comments