Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.30 KB | None | 0 0
  1. include('Utilities.js');
  2.  
  3. init = function() {
  4. /** @namespace
  5. * Functions to execute arrays comparison.
  6. * @requires Library: {@link Utilities}
  7. */
  8. Compare = {
  9.  
  10. /**
  11. * Compare two arrays
  12. * @author Anna Klimovskaya aklimovskaya@luxoft.com
  13. * @author Andrey Oleynik avoleynik@luxoft.com
  14. * @memberof Compare
  15. * @param {array} array1
  16. * First array to be compared
  17. * @param {array} array2
  18. * Second array to be compared
  19. * @return array of differences [[arra1][arra2]]
  20. * @example
  21. * // return [[],[1,3,2]]
  22. * arr1 = [
  23. * [1,2,3],
  24. * [1,2,3],
  25. * [1,2,4]
  26. * ]
  27. * arr2 = [
  28. * [1,2,3],
  29. * [1,2,4],
  30. * [1,2,3],
  31. * [1,3,2],
  32. * ]
  33. * Compare.compareArrays(arr1, arr2)
  34. */
  35. compareArrays: function (array1, array2) {
  36. function makeDictionary(array2D) {
  37. var hashTable = {};
  38. array2D.forEach(
  39. function(line){
  40. var hash = line.join(";");
  41. if (hashTable.hasOwnProperty(hash)) {
  42. hashTable[hash]["counter"]++;
  43. }
  44. else {
  45. hashTable[hash] = {
  46. "value": line,
  47. "counter": 1
  48. };
  49. }
  50. }
  51. );
  52. return hashTable;
  53. }
  54.  
  55. function compareDictionaries (dict1, dict2) {
  56. var diffArray = [];
  57. for (hash in dict1) {
  58. var diff = dict1[hash]["counter"];
  59. if (dict2.hasOwnProperty(hash)) {
  60. diff = diff - dict2[hash]["counter"];
  61. }
  62. if (diff > 0) {
  63. for (var i = 0; i < diff; i++) {
  64. diffArray.push(dict1[hash]["value"]);
  65. }
  66. }
  67. }
  68. return diffArray;
  69. }
  70.  
  71. var dictA = makeDictionary(array1);
  72. var dictB = makeDictionary(array2);
  73. return [compareDictionaries(dictA, dictB),
  74. compareDictionaries(dictB, dictA)];
  75. },
  76.  
  77. /**
  78. * Make dictionary for list comparison with line indentificator
  79. * @author Andrey Oleynik avoleynik@luxoft.com
  80. * @memberof Compare
  81. * @param {array} dict
  82. * Array specified by tester, a list of properties of a single service
  83. * e.g. [<ChannelName>, <LCN>, <Type>]
  84. * @param {array} compare
  85. * Array of service properties to be compared with "dict".
  86. * Columns of both arrays must correspond to each other.
  87. * @param {number} frInd
  88. * Index of the field with frequency
  89. * @param {number} frRange
  90. * Acceptable range of comparing flexibility
  91. * @param {number} sbInd
  92. * Index of the field with symbolrate
  93. * @param {number} sbRange
  94. * Acceptable range of comparing flexibility in %
  95. * @return {boolean}
  96. * "True" if both arrays are equal, "false" otherwise; "DNC" counts as a
  97. * wildcard, i. e. fields with "DNC" are just ignored during comparison.
  98. */
  99. compareServices: function (refer, compare, frInd, frRange, sbInd, sbRange) {
  100. var referCopy = refer.slice();
  101. var compareCopy = compare.slice();
  102. var frInd = Number(frInd) || referCopy.length;
  103. var frRange = Number(frRange) || 0;
  104. var sbInd = Number(sbInd) || referCopy.length;
  105. var sbRange = Number(sbRange) || 0;
  106.  
  107. for (var k = referCopy.length-1; k >= 0; k--) {
  108. if (referCopy[k] == "DNC") {
  109. referCopy.splice((k), 1);
  110. compareCopy.splice((k), 1);
  111. if (k < frInd) {
  112. frInd--;
  113. }
  114. if (k < sbInd) {
  115. sbInd--;
  116. }
  117. }
  118. }
  119. return (
  120. referCopy.length == compareCopy.length &&
  121. referCopy.every(
  122. function(item, index) {
  123. if ((index === frInd) && (compareCopy[index] != "NA")) {
  124. return (Math.abs(Number(compareCopy[index]) - Number(item)) <= frRange);
  125. }
  126. else if ((index === sbInd) && (compareCopy[index] != "NA")) {
  127. range = Number(item)*sbRange/100;
  128. return (Math.abs(Number(compareCopy[index]) - Number(item)) <= range);
  129. }
  130. else {
  131. return (item === compareCopy[index]);
  132. // To compare char by char
  133. /* for (var i = 0; i < item.length; i++) {
  134. if (item[i] !== compareCopy[index][i]) {
  135. Utilities.print("DEBUG: item is " + item);
  136. Utilities.print("DEBUG: compareCopy[index] is " + compareCopy[index]);
  137. Utilities.print("Character number " + i + ", '###" + item[i].charCodeAt() + "###'");
  138. Utilities.print("Character number " + i + ", '###" + compareCopy[index][i].charCodeAt() + "###'");
  139. return false;
  140. }
  141. }
  142. return true; */
  143. }
  144. }
  145. )
  146. );
  147. },
  148.  
  149. /**
  150. * Make dictionary for list comparison with line indentificator
  151. * @author Anna Klimovskaya aklimovskaya@luxoft.com
  152. * @author Andrey Oleynik avoleynik@luxoft.com
  153. * @memberof Compare
  154. * @param {array} expected
  155. * 2D array specified by tester, every row of that
  156. * is a list of properties of a single service
  157. * e.g. [[<ChannelName1>, <LCN1>, <Type1>],
  158. * [<ChannelName2>, <LCN2>, <Type2>],
  159. * ...
  160. * [<ChannelName#N>, <LCN#N>, <Type#N>]]
  161. * @param {array} actual
  162. * 2D array of services returned by a TV set; first columns of
  163. * this array must corrrespond to columns of "expected" array
  164. * e.g. [[<ChannelName1>, <LCN1>, <Type1>, <InternalIndex1>],
  165. * [<ChannelName2>, <LCN2>, <Type2>, <InternalIndex2>],
  166. * ...
  167. * [<ChannelName#N>, <LCN#N>, <Type#N>, <InternalIndex#N>]]
  168. * @param {function} idFunction
  169. * Rule for identifier definitions
  170. * (should return an identifier for input arrays),
  171. * e.g. function (arrayRow) {return arrayRow[1];}
  172. * The function in example returns value of first element for every service.
  173. * For example arrays it will be LCN.
  174. * @param {number} condInd
  175. * Index of the field to be "flexibly" compared
  176. * @param {number} range
  177. * Acceptable range of comparing flexibility
  178. * @return {array}
  179. * A hash map (or "identifier" map) containing an array of lists
  180. * for every identifier; each of these lists has three keys:
  181. * "value", "expected" and "actual".
  182. * -- "value" key contains rows from input arrays.
  183. * -- "expected" key contains expected number of corresponding rows.
  184. * -- "actual" key contains actual number of corresponding rows.
  185. * If there are several rows with the same identifier, new list is added
  186. * to the array corresponding to generated identifier for each row.
  187. * If there is no rows with the given identifier in one of arrays,
  188. * then corresponding counter key ("expected" or "actual") has zero value.
  189. * @example
  190. * //Note: Orders of rows in arrays are not required to match.
  191. * {
  192. * "LCN1":{
  193. * expected: [<ChannelName1>, <LCN1>, <Type1>],
  194. * actual: [<ChannelName1>, <LCN1>, <Type1>, <InternalIndex1>]
  195. * },
  196. * "LCN#I":{
  197. * expected: [<ChannelName#I>, <LCN#I>, <Type#I>],
  198. * actual: []
  199. * },
  200. * "LCN#K":{
  201. * expected: [],
  202. * actual: [<ChannelName#K>, <LCN#K>, <Type#K>, <InternalIndex#K>]
  203. * },
  204. *}
  205. */
  206. makeDictionary: function (expected, actual, idFunction, condInd, range, sbInd, sbRange) {
  207. var dict = {};
  208.  
  209. expected.forEach(
  210. function(line) {
  211. var id = idFunction(line);
  212. if (!dict.hasOwnProperty(id)) {
  213. dict[id] = [];
  214. }
  215.  
  216. if (!dict[id].some(
  217. function(item) {
  218. if (Compare.compareServices(line, item["value"], condInd, range, sbInd, sbRange)) {
  219. item["expected"]++;
  220. return true;
  221. }
  222. }
  223. )) {
  224. var newLine = {
  225. "value": line,
  226. "expected": 1,
  227. "actual": 0
  228. };
  229. dict[id].push(newLine);
  230. }
  231. }
  232.  
  233. );
  234.  
  235. actual.forEach(
  236. function(line) {
  237. var id = idFunction(line);
  238. if (!dict.hasOwnProperty(id)) {
  239. dict[id] = [];
  240. }
  241.  
  242. // In case of additional field (unique identifier)
  243. // we need to skip it during comparison, but also
  244. // store it in a separate field (Scan.js needs it)
  245. var uuid = null;
  246. if (typeof(expected[0]) == "undefined"){
  247. expected[0] = []
  248. }
  249.  
  250. // If some test fails try to uncomment this block
  251. var ldiff = line.length - expected[0].length;
  252. //Utilities.print("ldiff: " + ldiff);
  253. if (ldiff > 0) {
  254. // // For an unknown reason, without next ridiculous line,
  255. // // code just hangs up and stays there. Mysterious.
  256. line = line.slice();
  257. //Utilities.print("line: " + line);
  258. //Utilities.print("uuid: " + uuid);
  259. //for(var keys = Object.keys(line), i = 0, end = keys.length; i < end; i++) {
  260. //var key = keys[i], value = line[key];
  261. //Utilities.print("KEY " + key)
  262. //Utilities.print("VALUE " + value)
  263. //if (value.match("ch")) {
  264. //Utilities.print("CH IN VALUE!!!!")
  265. //line.pop(value)
  266. //}
  267. // do what you need to here, with index i as position information
  268. //};
  269.  
  270. //Utilities.print("LINE IS " + line)
  271. //Function below delete the last element after autoscan and in test of verification of scrambled services - incorrect.
  272. //And delete additional last element for manual scan - correct.
  273. if (line.length == 9 || line.length == 7) {
  274. uuid = line.pop();
  275. }
  276. // // If there's more than one "extra" field in actual line
  277. //line.length = expected[0].length;
  278. }
  279.  
  280. if (!dict[id].some(
  281. function(item) {
  282. if (Compare.compareServices(item["value"], line, condInd, range, sbInd, sbRange)) {
  283. item["actual"]++;
  284. item["uuid"] = uuid;
  285. return true;
  286. }
  287. }
  288. )) {
  289. var newLine = {
  290. "value": line,
  291. "expected": 0,
  292. "actual": 1,
  293. "uuid": uuid
  294. };
  295. dict[id].push(newLine);
  296. }
  297. }
  298. );
  299.  
  300. return dict;
  301. },
  302.  
  303. /**
  304. * Pretty print of list comparison.
  305. * The function prints all distinctions in expected and actual results
  306. * @author Anna Klimovskaya aklimovskaya@luxoft.com
  307. * @author Andrey Oleynik avoleynik@luxoft.com
  308. * @memberof Compare
  309. * @param {array} dictionary
  310. * Hash map containing expected (defined by tester) and
  311. * actual (returned by TV set) results; created by makeDictionary()
  312. * @param {array} headers
  313. * Array of column names in expected result
  314. * (in corresponding oreder), e.g. ["Name", "LCN", "ONID", "TSID", "SID"
  315. * @param {string} {checkType="INFO"|"WARN"|"RSLT"|"#ERROR "}
  316. * Type of verification. For "RSLT", failed result will be marked by
  317. * "#VERIFICATION FAILED", for others types failed result will be marked
  318. * by lable. Equality result will be marked as "#VERIFICATION PASSED".
  319. * Default value is "RSLT".
  320. * @return {true|false}
  321. * The function return "true" if results are matched and "false" otherwise.
  322. * @requires Library: {@link Utilities}
  323. */
  324. compareDictionaries: function (dictionary, headers, checkType) {
  325. var checkType = checkType || "#VERIFICATION FAILED";
  326. if (checkType == "RSLT") {
  327. checkType = "#VERIFICATION FAILED";
  328. }
  329.  
  330. function makeResult(dictionary) {
  331. var dict = {};
  332.  
  333. for (hash in dictionary) {
  334. dictionary[hash].forEach(
  335. function(line) {
  336. var diff = line["expected"] - line["actual"];
  337. if (diff != 0) {
  338. if (!dict.hasOwnProperty(hash)) {
  339. dict[hash] = {};
  340. }
  341. if (diff > 0) {
  342. if (!dict[hash].hasOwnProperty("expected")) {
  343. dict[hash].expected = [];
  344. }
  345. for (var i = 0; i < diff; i++) {
  346. dict[hash].expected.push(line["value"]);
  347. }
  348. }
  349. else {
  350. if (!dict[hash].hasOwnProperty("actual")) {
  351. dict[hash].actual = [];
  352. }
  353. for (var i = diff; i < 0; i++) {
  354. dict[hash].actual.push(line["value"]);
  355. }
  356. }
  357. }
  358. }
  359. );
  360. }
  361. return dict;
  362. }
  363.  
  364. function prettyPrint(arr) {
  365. return arr.reduce(
  366. function(accum, item) {
  367. return accum + '"' + item + '", '
  368. },
  369. "[ ").slice(0, -2) + " ],";
  370. }
  371.  
  372. function printResult(dict) {
  373. var result = {
  374. "notFound": {},
  375. "notExpected": {},
  376. "changed": {}
  377. };
  378.  
  379. for (key in dict) {
  380. if (!dict[key].hasOwnProperty("actual")) {
  381. result.notFound[key] = dict[key];
  382. }
  383. else if (!dict[key].hasOwnProperty("expected")) {
  384. result.notExpected[key] = dict[key];
  385. }
  386. else {
  387. result.changed[key] = dict[key];
  388. }
  389. }
  390.  
  391. if (Object.keys(result.notFound).length != 0) {
  392. Utilities.print("++++++++++++++++++");
  393. Utilities.print(checkType + ": some rows were NOT FOUND!"
  394. + "\nFields: " + headers.join(", "));
  395. for (key in result.notFound) {
  396. Utilities.print(key + ":");
  397. result.notFound[key].expected.forEach(
  398. function(line) {
  399. Utilities.print("# " + prettyPrint(line));
  400. }
  401. );
  402. }
  403. Utilities.print();
  404. }
  405.  
  406. if (Object.keys(result.notExpected).length != 0) {
  407. Utilities.print("++++++++++++++++++");
  408. Utilities.print(checkType + ": some rows were NOT EXPECTED!"
  409. + "\nFields: " + headers.join(", "));
  410. for (key in result.notExpected) {
  411. Utilities.print(key + ":");
  412. result.notExpected[key].actual.forEach(
  413. function(line) {
  414. Utilities.print("# " + prettyPrint(line));
  415. }
  416. );
  417. }
  418. Utilities.print();
  419. }
  420.  
  421. if (Object.keys(result.changed).length != 0) {
  422. Utilities.print("++++++++++++++++++");
  423. Utilities.print(checkType + ": some rows were CHANGED!"
  424. + "\nFields: " + headers.join(", "));
  425. for (key in result.changed) {
  426. Utilities.print("\n" + key + ":");
  427. Utilities.print("# Expected result:");
  428. result.changed[key].expected.forEach(
  429. function(line) {
  430. Utilities.print("# " + prettyPrint(line));
  431. }
  432. );
  433. Utilities.print("# Actual result:");
  434. result.changed[key].actual.forEach(
  435. function(line) {
  436. Utilities.print("# " + prettyPrint(line));
  437. }
  438. );
  439. }
  440. Utilities.print();
  441. }
  442. }
  443.  
  444. var resultDict = makeResult(dictionary);
  445.  
  446. if (Object.keys(resultDict).length == 0) {
  447. Utilities.print("#VERIFICATION PASSED: Results are identical.");
  448. return true;
  449. }
  450. else {
  451. printResult(resultDict);
  452. return false;
  453. }
  454. }
  455.  
  456. }
  457. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement