diff --git a/README.md b/README.md index 9ce4bbe..fd8eaec 100644 --- a/README.md +++ b/README.md @@ -35,15 +35,19 @@ of the Mapcode library by Rijn Buve and Matthew Lowden. # Javascript Files for Mapcode Support +The following files provide the Javascript interfaces for mapcodes: + mapcode.js - Key routines for mapcode support ndata.js - Data table for mapcode support sample.html - Sample code to interpret / generate mapcodes ctrynams.js - Optional js array with the names of territories (in English) +To run the Javascript unit tests, simply open the file `unittest/unittest/html`. + # Version History -* 2.2 +* 2.2.0 Solved 1-microdegree gap in a few spots on Earth, noticable now extreme precision is possible; diff --git a/mapcode.js b/mapcode.js index a61fc99..1c3c9f1 100644 --- a/mapcode.js +++ b/mapcode.js @@ -12,7 +12,7 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - */ + */ var entity_iso = [ 'VAT','MCO','GIB','TKL','CCK','BLM','NRU','TUV','MAC','SXM', 'MAF','NFK','PCN','BVT','BMU','IOT','SMR','GGY','AIA','MSR', @@ -116,11 +116,11 @@ var parents2 = "US,IN,CA,AU,MX,BR,RU,CN,"; var ccode_start = 112; // NLD var mapcode_cversion = "2.0.2"; -var mapcode_dataversion = "2.2"; +var mapcode_dataversion = "2.2.0"; // *************************** mapcode_org ********************* -var mapcode_javaversion = '2.2.1/Data' + mapcode_dataversion; +var mapcode_javaversion = '2.2.2/Data' + mapcode_dataversion; /// PRIVATE returns string without leading spaces and plus-signs, and trailing spaces function trim(str) { @@ -670,7 +670,7 @@ function mzSetFromFractions(y, x, yDelta, xDelta) { fmaxx: x + xDelta, fminy: y + 1 + yDelta, // y+yDelta can NOT be represented fmaxy: y + 1 // y CAN be represented - }; + }; } else { return { diff --git a/unittest/test_encodes.js b/unittest/test_encodes.js index 791e655..95aec53 100644 --- a/unittest/test_encodes.js +++ b/unittest/test_encodes.js @@ -1,3 +1,18 @@ +/* + * Copyright (C) 2014-2015 Stichting Mapcode Foundation (http://www.mapcode.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ var testdata = [ @@ -22,14 +37,14 @@ var testdata = [ "", 55.015025, 20.950025, 0,0, "", 21.6199995, -102.02419949999999, 0,0, "", 21.6200035, -102.0242375, 0,0, -"", 29.727055, 73.87498650000001,0,0 , +"", 29.727055, 73.87498650000001,0,0 , "", 29.727025, 73.87502499999999,0,0, "", 28.679995, 78.71705600000000,0,0, "", 28.680025, 78.71702500000001,0,0, "", 5.785780,169.79175, 0,0 , // grid "", -9.34015920,-171.99626665, 0,0 , // encompassing "", 49.4632595, -2.55989, 0,0 , // inner grid -"", 49.4632625, -2.5599018, 0,0 , // old +"", 49.4632625, -2.5599018, 0,0 , // old "", 47.1495405, 9.5319995, 0,0 , // inner grid AUT "", 41.9035, 12.45200000000000 , 0,0, "", 12.168921, -68.26226699999999, 0,0, @@ -15883,6 +15898,6 @@ var testdata = [ "CN-XJ 123.XXX",46.1175215,84.492761000,3,7, "CN-XJ 12.XXXX",45.925468,80.918028500,2,5, "CN-XJ 0123.XX",40.917864,79.977318500,3,6, - + -1]; diff --git a/unittest/test_territories.js b/unittest/test_territories.js index a6042a7..a7ebbe9 100644 --- a/unittest/test_territories.js +++ b/unittest/test_territories.js @@ -1,3 +1,18 @@ +/* + * Copyright (C) 2014-2015 Stichting Mapcode Foundation (http://www.mapcode.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ function test_territories() { test_territory("AAA", 533, 0, 0, 0); diff --git a/unittest/unittest.html b/unittest/unittest.html index d1084e7..3a4a002 100644 --- a/unittest/unittest.html +++ b/unittest/unittest.html @@ -1,4 +1,19 @@ + Mapcode Javascript Unit Test @@ -9,13 +24,15 @@ -Mapcode Javascript Unit Test 2.2.1
+

Mapcode Javascript Unit Tests

@@ -44,7 +61,7 @@ out('convertToAlphabet(convertToAlphabet("' + str + '",' + i + '))= "' + dec + '", expected "' + expect + '"'); } - // see if E/U voweled mapcodes (re)convert as expected + // see if E/U vowelled mapcodes (re)convert as expected str = "OMN 112.3AU"; expect = str; nrTests++; @@ -59,8 +76,9 @@ function printGeneratedMapcodes(r, name) { var n = r.length; var t = '   ' + n + ' results generated (' + name + '):'; - for (var i = 0; i < n; i++) + for (var i = 0; i < n; i++) { t += ' (' + r[i].fullmapcode + ')'; + } out(t); } @@ -98,8 +116,9 @@ // test that expected solution is there var foundlocal = 0; for (var i = 0; i < r.length; i++) { - if (r[i].fullmapcode.indexOf(str) == 0) + if (r[i].fullmapcode.indexOf(str) == 0) { foundlocal = 1; + } } if (!foundlocal) { nrErrors++; @@ -121,7 +140,7 @@ } } - // check that all global solutions are within 9 milimeters of coordinate + // check that all global solutions are within 9 millimeters of coordinate for (precision = 0; precision <= 8; precision++) { r = encodeWithPrecision(y, x, precision); for (i = 0; i < r.length; i++) { @@ -233,10 +252,10 @@ "NLD 123.U45", // U in invalid position "NLD 123.1UE", // UE illegal vowel-encode "NLD 123.1UU", // UU illegal - "NLD x23.1A0", // A0 with nondigit - "NLD 1x3.1A0", // A0 with nondigit - "NLD 12x.1A0", // A0 with nondigit - "NLD 123.xA0", // A0 with nondigit + "NLD x23.1A0", // A0 with non-digit + "NLD 1x3.1A0", // A0 with non-digit + "NLD 12x.1A0", // A0 with non-digit + "NLD 123.xA0", // A0 with non-digit "NLD 123.1U#", // U# "NLD ZZ.ZZ", // nameless out of range @@ -249,8 +268,9 @@ var i; for (i = 0; ; i++) { var str = badcodes[i]; - if (str == 'end') + if (str == 'end') { break; + } nrTests++; var p = decode(str); @@ -261,12 +281,12 @@ } } - // test encode/decodes from testdata array; returns nonzero if more tests are needed + // test encode/decodes from test data array; returns nonzero if more tests are needed var next_encode_to_test = 0; function test_encodes_decodes() { var tests_per_timeslot = 50; for (var i = next_encode_to_test; testdata[i] != -1; i += 5) { - if (nrErrors > 20) { + if (nrErrors > 0) { out('Too many errors!'); return 0; } @@ -307,11 +327,13 @@ return 0; } - // perform tests on alphacodes (used from test_territories.js) + // perform tests on alpha codes (used from test_territories.js) function test_territory(alphacode, tc, isAlias, needsParent, tcParent) { var ccode = tc - 1; var ccodeParent = tcParent - 1; - if (nrErrors > 30) return; + if (nrErrors > 30) { + return; + } nrTests++; var tn = getTerritoryNumber(alphacode, needsParent ? ccodeParent : 0); @@ -343,8 +365,8 @@ "ATA", -90, 0, 1, // in data version 2.2, ATA -90,0 has ZZ.ZZ and multiple borders "ATA", -70, 0, 0, - "USA", 31, -70, 0, // interational waters (not in state) - "MEX", 19, -115, 0, // interational waters (not in state) + "USA", 31, -70, 0, // international waters (not in state) + "MEX", 19, -115, 0, // international waters (not in state) "MEX", 18.358525, -114.722672, 0, // Isla Clarion, not in a state "MX-ROO", 20, -87, 0, // just in ROO "MX-ROO", 20, -87.3, 0, // in ROO because in MEX @@ -428,72 +450,73 @@ function territory_code_tests() { var testdata = [ // expected answer, context, string - "BR-AL", "BRA", "AL", - "VAT", , "", - "", , "R", - "", , "RX", - "", , "RXX", - "RUS", , "RUS", - "", , "RUSSIA", - "USA", , "US", - "USA", , "USA", - "USA", , "usa", - "USA", , " usa ", - "", , "999", - "", , "-44 33", - "", , "666", - "USA", , "410", - "USA", , " 410 ", - "USA", , "410 MORE TEXT", - "USA", , "US CA", - "", , "US-TEST", - "USA", , "US OTHER TEXT", - "USA", , " US OTHER TEXT ", - "US-CA", , "US-CA", - "US-CA", , "US-CA OTHER TEXT", - "US-CA", , "USA-CA", - "RU-TT", , "RUS-TAM", - "", , "RUS-TAMX", - "RU-TT", , "RUS-TAM X OTHER TEXT", - "RU-AL", "rus", "AL", - "RU-AL", "RUS", "AL", - "RU-AL", "ru-tam", "AL", - "RU-AL", "RU-TAM", "AL", - "US-AL", "US", "AL", - "US-AL", "US-CA", "AL", - -9]; - - for (var i = 0; testdata[i] != -9; i += 3 ) { - var tc = getTerritoryAlphaCode(getTerritoryNumber(testdata[i + 2], testdata[i + 1])); - nrTests++; - if (tc != testdata[i]) - { - nrErrors++; - out('*** ERROR *** getTerritoryNumber("' + testdata[i + 2] + '", ' + - testdata[i + 1] + ')=' + tc + ', expected ' + testdata[i]); + "BR-AL", "BRA", "AL", + "VAT", , "", + "", , "R", + "", , "RX", + "", , "RXX", + "RUS", , "RUS", + "", , "RUSSIA", + "USA", , "US", + "USA", , "USA", + "USA", , "usa", + "USA", , " usa ", + "", , "999", + "", , "-44 33", + "", , "666", + "USA", , "410", + "USA", , " 410 ", + "USA", , "410 MORE TEXT", + "USA", , "US CA", + "", , "US-TEST", + "USA", , "US OTHER TEXT", + "USA", , " US OTHER TEXT ", + "US-CA", , "US-CA", + "US-CA", , "US-CA OTHER TEXT", + "US-CA", , "USA-CA", + "RU-TT", , "RUS-TAM", + "", , "RUS-TAMX", + "RU-TT", , "RUS-TAM X OTHER TEXT", + "RU-AL", "rus", "AL", + "RU-AL", "RUS", "AL", + "RU-AL", "ru-tam", "AL", + "RU-AL", "RU-TAM", "AL", + "US-AL", "US", "AL", + "US-AL", "US-CA", "AL", + -9]; + + for (var i = 0; testdata[i] != -9; i += 3) { + var tc = getTerritoryAlphaCode(getTerritoryNumber(testdata[i + 2], testdata[i + 1])); + nrTests++; + if (tc != testdata[i]) { + nrErrors++; + out('*** ERROR *** getTerritoryNumber("' + testdata[i + 2] + '", ' + + testdata[i + 1] + ')=' + tc + ', expected ' + testdata[i]); } } } - var myVar = setInterval(function () { stepper() }, 20); + var myVar = setInterval(function () { + stepper() + }, 20); var dostep = 0; function stepper() { - switch (dostep++) { + switch (dostep) { case 0: - out('JavaScript version ' + mapcode_javaversion); + out('Mapcode Javascript version ' + mapcode_javaversion); break; case 1: - out('
Alphabet tests'); + out('
Alphabet tests'); break; case 2: alphabet_tests(); break; case 3: - out('
Distance tests'); + out('
Distance tests'); distance_tests(); - out('
Territory tests'); + out('
Territory tests'); out((ccode_earth + 1) + ' territories'); test_territory_insides(); break; @@ -502,31 +525,42 @@ test_territories(); break; case 5: - out('
Failing decode tests'); + out('
Incorrect format tests'); break; case 6: test_failing_decodes(); break; case 7: - out('
Encode/Decode tests'); + out('
Encode/decode tests'); break; case 8: - if (test_encodes_decodes()) dostep--; + if (test_encodes_decodes()) { + dostep--; + } break; case 9: - out('
Edge-case Encode/Decode tests'); + out('
Edge-case encode/decode tests'); break; case 10: - if (test_corner_encodes()) dostep--; + if (test_corner_encodes()) { + dostep--; + } break; case 11: - out('
Done.'); + out('
Done
'); out('Executed ' + nrTests + ' tests, found ' + nrErrors + ' errors'); + if ((nrWarnings > 0) || (nrErrors > 0)) { + out("UNIT TEST FAILED!"); + } + else { + out("Unit tests passed"); + } break; default: clearInterval(myVar); break; } + dostep++; }