Skip to content

Commit 2f0e566

Browse files
committed
Adds units test for shortest code in getTerritoryAlphaCode
1 parent 1b59c43 commit 2f0e566

1 file changed

Lines changed: 42 additions & 5 deletions

File tree

unittest/unittest.html

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ <h1>Mapcode Javascript Unit Tests</h1>
411411

412412
// test encode/decodes from test data array; returns nonzero if more tests are needed
413413
var next_encode_to_test = 0;
414+
414415
function test_encodes_decodes() {
415416
var tests_per_timeslot = 50;
416417
for (var i = next_encode_to_test; testdata[i] != -1; i += 5) {
@@ -431,6 +432,7 @@ <h1>Mapcode Javascript Unit Tests</h1>
431432

432433

433434
var next_corner_to_test = 0;
435+
434436
function test_corner_encodes() {
435437
var tests_per_timeslot = 20;
436438
var last = dataLastRecord(ccode_earth);
@@ -468,7 +470,7 @@ <h1>Mapcode Javascript Unit Tests</h1>
468470

469471
if (tn != ccode) {
470472
nrErrors++;
471-
out('*** ERROR *** getTerritoryNumber(' + alphacode + (needsParent ? ',' + getTerritoryAlphaCode(ccodeParent) : "") + ')=' + tn + '=' + getTerritoryAlphaCode(tn) + ' but expected ' + ccode + '=' + getTerritoryAlphaCode(ccode));
473+
out('*** ERROR *** getTerritoryNumber(' + alphacode + (needsParent ? ',' + getTerritoryAlphaCode(ccodeParent) : "") + ')=' + tn + '=' + getTerritoryAlphaCode(tn) + ' but expected ' + ccode + '=' + getTerritoryAlphaCode(ccode));
472474
}
473475

474476
if (needsParent == 0 && isAlias == 0 && (alphacode.length <= 3 || alphacode.charAt(3) != '-')) {
@@ -577,11 +579,11 @@ <h1>Mapcode Javascript Unit Tests</h1>
577579

578580
function territory_code_tests() {
579581
var testdata = [
582+
// expected answer, context, string
580583
"ASM", "USA", "AS",
581584
"MX-AGU", , "AG",
582585
"MX-TAM", , "MEX-TAM",
583586
"CXR", , "AUS-CX",
584-
// expected answer, context, string
585587
"BR-AL", "BRA", "AL",
586588
"VAT", , "",
587589
"", , "R",
@@ -615,24 +617,58 @@ <h1>Mapcode Javascript Unit Tests</h1>
615617
"RU-AL", "RU-TAM", "AL",
616618
"US-AL", "US", "AL",
617619
"US-AL", "US-CA", "AL",
618-
-9];
620+
-999];
619621

620-
for (var i = 0; testdata[i] != -9; i += 3) {
622+
for (var i = 0; testdata[i] != -999; i += 3) {
621623
var tc = getTerritoryAlphaCode(getTerritoryNumber(testdata[i + 2], testdata[i + 1]));
622624
nrTests++;
623625
if (tc != testdata[i]) {
624626
nrErrors++;
625-
out('*** ERROR *** getTerritoryNumber("' + testdata[i + 2] + '", ' +
627+
out('*** ERROR *** getTerritoryAlphaCode("' + testdata[i + 2] + '", ' +
626628
testdata[i + 1] + ')=' + tc + ', expected \"' + testdata[i] + '\"');
627629
}
628630
}
629631
}
630632

633+
function territory_alphacode_tests() {
634+
var testdata = [
635+
// expected answer, territory, 0=local;1=international;2=shortest
636+
"CA", "US-CA", 0, // CA is not ambiguous.
637+
"US-CA", "US-CA", 1,
638+
"CA", "US-CA", 2,
639+
"CAM", "MX-CAM", 0, // Test for -CA and -CAM.
640+
"MX-CAM", "MX-CAM", 1,
641+
"CAM", "MX-CAM", 2,
642+
"MN", "US-MN", 0,
643+
"US-MN", "US-MN", 1,
644+
"US-MN", "US-MN", 2,
645+
"MN", "IN-MN", 0, // MN is ambiguous as a state.
646+
"IN-MN", "IN-MN", 1,
647+
"IN-MN", "IN-MN", 2,
648+
"IN", "US-IN", 0, // IN is ambigious as a state/country.
649+
"US-IN", "US-IN", 1,
650+
"US-IN", "US-IN", 2,
651+
"NLD", "NLD", 0, // NLD is never ambiguous.
652+
"NLD", "NLD", 1,
653+
"NLD", "NLD", 2,
654+
-999
655+
];
656+
for (var i = 0; testdata[i] != -999; i += 3) {
657+
var tc = getTerritoryAlphaCode(getTerritoryNumber(testdata[i + 1]), testdata[i + 2]);
658+
nrTests++;
659+
if (tc != testdata[i]) {
660+
nrErrors++;
661+
out('*** ERROR *** getTerritoryAlphaCode("' + testdata[i + 1] + '", ' + testdata[i + 2] + ')=' + tc +
662+
', expected \"' + testdata[i] + '\"');
663+
}
664+
}
665+
}
631666

632667
var myVar = setInterval(function () {
633668
stepper()
634669
}, 20);
635670
var dostep = 0;
671+
636672
function stepper() {
637673
switch (dostep) {
638674
case 0:
@@ -653,6 +689,7 @@ <h1>Mapcode Javascript Unit Tests</h1>
653689
break;
654690
case 4:
655691
territory_code_tests();
692+
territory_alphacode_tests();
656693
test_territories();
657694
break;
658695
case 5:

0 commit comments

Comments
 (0)