|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2019 The TensorFlow Datasets Authors. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +"""Tests for tensorflow_datasets.core.hashing.""" |
| 17 | + |
| 18 | +from __future__ import absolute_import |
| 19 | +from __future__ import division |
| 20 | +from __future__ import print_function |
| 21 | + |
| 22 | +import string |
| 23 | + |
| 24 | +from tensorflow_datasets import testing |
| 25 | +from tensorflow_datasets.core import hashing |
| 26 | + |
| 27 | +EXPECTED_ASCII_HASHES = [ |
| 28 | + 10863254463029944905, 17270894748891556580, 1421958803217889556, |
| 29 | + 2755936516345535118, 10743397483099227609, 14847904966537228119, |
| 30 | + 8418086691718335568, 11915783202300667430, 9716548853924804360, |
| 31 | + 1754241953508342506, 3694815635762763843, 3596675173038026136, |
| 32 | + 8070480893871644201, 10828895317094483299, 6744895540894632903, |
| 33 | + 14841125270424448140, 17688157251436176611, 233329666533924104, |
| 34 | + 12405195962649360880, 6834955004197680363, 13969673605019971927, |
| 35 | + 7535964442537143799, 4233129741512941149, 6982149393400456965, |
| 36 | + 6390938501586896152, 10691964138414231865, 2507792285634992701, |
| 37 | + 8595505165718044977, 12668683514780208634, 4572719121647279127, |
| 38 | + 3793178347273963799, 12176803637319179781, 8203705581228860887, |
| 39 | + 6621546828295374121, 10489668860986262979, 2912571526136585784, |
| 40 | + 7938998531316856084, 9873236742883981124, 11452724586647173125, |
| 41 | + 17125311730203373552, 14019180516172084896, 4703666581826301487, |
| 42 | + 855367633508639125, 14196452709219207778, 12230774050365420318, |
| 43 | + 2537184170178946005, 7687283993009238581, 8820003036235826491, |
| 44 | + 17283458755135480410, 17416811303727010451, 5922709015603821319, |
| 45 | + 14428075881971433291] |
| 46 | + |
| 47 | +EXPECTED_INT_HASHES = [ |
| 48 | + 13163753087252323914, 5003827105613308882, 11449545338359147399, |
| 49 | + 3672830208859661989, 5406800756778728304, 14481025309921804611, |
| 50 | + 1609946449970207933, 8255655750251093705, 8491335656787965458, |
| 51 | + 614618433269745394, 13284406229155908416, 15288099744915406683, |
| 52 | + 15843726386018726000, 5119555216682145351, 18142174963634300418, |
| 53 | + 9628501536262262909, 6944376202371927192, 14434159142587098114, |
| 54 | + 9198994799148619777, 10976198626365556062, 12201794547984161272, |
| 55 | + 3793153257673850654, 17671034425519684114, 1052793885524652273, |
| 56 | + 6624479012289984384, 8863054234635171593, 4965745346604460244, |
| 57 | + 9391177234155550519, 4717670777148145883, 17524121804784260174, |
| 58 | + 11037926627716248709, 6960985957824457329, 12195906204051370437, |
| 59 | + 10975328135781691390, 5730073803446725122, 13712792850809427923, |
| 60 | + 4455483863044151629, 3518672581294300691, 15747605586304671771, |
| 61 | + 13668305533495453291, 4654232860820002596, 10574044313476412487, |
| 62 | + 11212237458977771261, 15365614270461858889, 13872585532843456912, |
| 63 | + 17241372482471269826, 3462651383276316179, 12647419365920702661, |
| 64 | + 1995464140987078702, 1972561720831881829, 3955328643960597520, |
| 65 | + 1027369990565220197, 11322815499836299743, 5956980248780520012, |
| 66 | + 18278096046037631584, 5241067853637136955, 7630275157338448032, |
| 67 | + 1913046367367276708, 7440126551217879907, 7220748048216444121, |
| 68 | + 5064805925883037540, 1318705738537093225, 2730963110225791297, |
| 69 | + 6920195161195209846, 4682001368639242156, 9166607120404080113, |
| 70 | + 11268721706256216334, 5379201623047735445, 15999685243572303930, |
| 71 | + 13046608731927560566, 16276928149450612660, 16298571539550440629, |
| 72 | + 17035045450101282343, 14240119263724925078, 9965315260615748500, |
| 73 | + 14921974451741066715, 3620887669157180415, 14499246414755411500, |
| 74 | + 188410546870139183, 14101909720529780551, 1623775225152586541, |
| 75 | + 1826999275929156985, 5289921295512723016, 151075781360207052, |
| 76 | + 17598366794955569210, 1171265316432012145, 104641658363814304, |
| 77 | + 9264688353594391671, 458105873437653640, 1830791798008018143, |
| 78 | + 15150529348822956655, 7610023982731034430, 12031109555968877759, |
| 79 | + 2814999125315447998, 14537302745610214253, 14150033554738292901, |
| 80 | + 1316273336836242886, 4973610113424219627, 11435740729903845598, |
| 81 | + 4598673630928554393] |
| 82 | + |
| 83 | + |
| 84 | +class HashingTest(testing.TestCase): |
| 85 | + |
| 86 | + def _assert_hashes(self, keys, expected_hashes): |
| 87 | + hashes = [hashing.hash_key(k) for k in keys] |
| 88 | + self.assertEqual(hashes, expected_hashes) |
| 89 | + |
| 90 | + def test_ints(self): |
| 91 | + ints = list(range(100)) |
| 92 | + hashing._CSIPHASH_AVAILABLE = False |
| 93 | + self._assert_hashes(ints, EXPECTED_INT_HASHES) |
| 94 | + hashing._CSIPHASH_AVAILABLE = True |
| 95 | + self._assert_hashes(ints, EXPECTED_INT_HASHES) |
| 96 | + |
| 97 | + def test_ascii(self): |
| 98 | + letters = string.ascii_lowercase + string.ascii_uppercase |
| 99 | + hashing._CSIPHASH_AVAILABLE = False |
| 100 | + self._assert_hashes(letters, EXPECTED_ASCII_HASHES) |
| 101 | + hashing._CSIPHASH_AVAILABLE = True |
| 102 | + self._assert_hashes(letters, EXPECTED_ASCII_HASHES) |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == '__main__': |
| 106 | + testing.test_main() |
0 commit comments