Skip to content

Commit 49def3c

Browse files
committed
Add hash tests
1 parent 56a7a2f commit 49def3c

3 files changed

Lines changed: 114 additions & 0 deletions

File tree

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,8 @@ _test-unicode-conformance = ["ureq"]
2020
[dependencies]
2121
chinese-number = { version = "0.7.7", default-features = false, features = ["number-to-chinese"], optional = true }
2222

23+
[dev-dependencies]
24+
siphasher = "1.0.2"
25+
2326
[build-dependencies]
2427
ureq = { version = "3.0.12", optional = true }

src/numeral_systems.rs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,10 @@ pub enum RepresentationError {
11551155

11561156
#[cfg(test)]
11571157
mod tests {
1158+
use std::hash::{Hash, Hasher};
1159+
1160+
use siphasher::sip128::{Hasher128, SipHasher13};
1161+
11581162
use super::{NamedNumeralSystem, NumeralSystem};
11591163

11601164
/// Makes sure shorthands correspond to the way the number one is
@@ -1332,4 +1336,104 @@ mod tests {
13321336
)
13331337
}
13341338
}
1339+
1340+
/// Compares the hashes of the first integers represented in each numeral
1341+
/// system to pre-computed values.
1342+
#[test]
1343+
fn test_numeral_systems() {
1344+
struct StableHasher(SipHasher13);
1345+
1346+
impl Hasher for StableHasher {
1347+
fn finish(&self) -> u64 {
1348+
self.0.finish()
1349+
}
1350+
1351+
fn write(&mut self, bytes: &[u8]) {
1352+
self.0.write(bytes);
1353+
}
1354+
1355+
fn write_usize(&mut self, i: usize) {
1356+
self.0.write_u64(i as u64);
1357+
}
1358+
}
1359+
1360+
fn compute_hash(system: NamedNumeralSystem) -> u128 {
1361+
let mut state = StableHasher(SipHasher13::new());
1362+
for i in 0..50_000 {
1363+
system.system().represent(i).map(|r| r.to_string()).hash(&mut state)
1364+
}
1365+
state.0.finish128().as_u128()
1366+
}
1367+
1368+
fn expected_hash(system: NamedNumeralSystem) -> u128 {
1369+
match system {
1370+
NamedNumeralSystem::Arabic => 233363652923672209674688099512602556474,
1371+
NamedNumeralSystem::CircledArabic => {
1372+
14788096368351499805674874468259519865
1373+
}
1374+
NamedNumeralSystem::DoubleCircledArabic => {
1375+
84846816834872732753601089381949808193
1376+
}
1377+
NamedNumeralSystem::LowerLatin => 338462384600087330263193927875970822818,
1378+
NamedNumeralSystem::UpperLatin => 63389938855801182654207252735381557455,
1379+
NamedNumeralSystem::LowerRoman => 320120650624228984391933034556134697794,
1380+
NamedNumeralSystem::UpperRoman => 179137825631358807472580756311985798892,
1381+
NamedNumeralSystem::LowerGreek => 286426313636684184647936794996618738517,
1382+
NamedNumeralSystem::UpperGreek => 266767054320463395696526156316564222710,
1383+
NamedNumeralSystem::LowerArmenian => {
1384+
118575058866853099370711220898739682550
1385+
}
1386+
NamedNumeralSystem::UpperArmenian => {
1387+
14157728964774965650431335537322548529
1388+
}
1389+
NamedNumeralSystem::Hebrew => 206914675362605565607546884904163595545,
1390+
NamedNumeralSystem::LowerSimplifiedChinese => {
1391+
111467758380137268027180550654359765178
1392+
}
1393+
NamedNumeralSystem::UpperSimplifiedChinese => {
1394+
245480392218028497842549251253255025420
1395+
}
1396+
NamedNumeralSystem::LowerTraditionalChinese => {
1397+
335477487643271707320761870063839694075
1398+
}
1399+
NamedNumeralSystem::UpperTraditionalChinese => {
1400+
97580884915630322847859767213149399933
1401+
}
1402+
NamedNumeralSystem::HiraganaAiueo => {
1403+
228263127493940549113355043662499568034
1404+
}
1405+
NamedNumeralSystem::HiraganaIroha => {
1406+
223752166294897561554884466357640039672
1407+
}
1408+
NamedNumeralSystem::KatakanaAiueo => {
1409+
159989562581792168649789815071020535332
1410+
}
1411+
NamedNumeralSystem::KatakanaIroha => {
1412+
199999534019736521402858209442755367027
1413+
}
1414+
NamedNumeralSystem::KoreanJamo => 65477685939649764827530478995838083425, // 21
1415+
NamedNumeralSystem::KoreanSyllable => {
1416+
24217153056183571894327643661698510954
1417+
}
1418+
NamedNumeralSystem::EasternArabic => {
1419+
277754701051910363703826860323053920831
1420+
}
1421+
NamedNumeralSystem::Persian => 6232158096065129450489636457808686806,
1422+
NamedNumeralSystem::Devanagari => 327133969362282954753636774557232534052,
1423+
NamedNumeralSystem::Bengali => 79096832028418218544110224478554962928,
1424+
NamedNumeralSystem::BengaliLetters => {
1425+
269999388716378396079918080520770981179
1426+
}
1427+
NamedNumeralSystem::Symbols => 88780534058354093087932015985325954737,
1428+
}
1429+
}
1430+
1431+
for system in NamedNumeralSystem::iter() {
1432+
assert_eq!(
1433+
expected_hash(system),
1434+
compute_hash(system),
1435+
"unexpected hash for `{system:?}` (left is expected, right is computed)",
1436+
)
1437+
}
1438+
}
13351439
}

0 commit comments

Comments
 (0)