testInt32x4.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. this.WScript.LoadScriptFile("..\\UnitTestFramework\\SimdJsHelpers.js");
  6. var sf0 = SIMD.Int32x4(1, -2, 3, 0);
  7. var sf1 = SIMD.Int32x4(1, -2, 3, 0);
  8. var sf2 = SIMD.Int32x4(1, -2, 13, 0);
  9. var sf3 = SIMD.Int32x4(0, Infinity, NaN, -0);
  10. function testConstructor()
  11. {
  12. ////////////////////
  13. //Constructor
  14. ///////////////////
  15. equal("object", typeof SIMD.Int32x4.prototype);
  16. equal("function", typeof SIMD.Int32x4.prototype.constructor);
  17. equal("function", typeof SIMD.Int32x4.prototype.toString);
  18. equal("function", typeof SIMD.Int32x4.prototype.toLocaleString);
  19. equal("function", typeof SIMD.Int32x4.prototype.valueOf);
  20. equal(SIMD.Int32x4, SIMD.Int32x4.prototype.constructor);
  21. descriptor = Object.getOwnPropertyDescriptor(SIMD.Int32x4, 'prototype');
  22. equal(false, descriptor.writable);
  23. equal(false, descriptor.enumerable);
  24. equal(false, descriptor.configurable);
  25. }
  26. function testToStringTag()
  27. {
  28. ///////////////////
  29. //SIMDConstructor.prototype [ @@toStringTag ]
  30. ///////////////////
  31. var sym = Symbol('sym');
  32. var symbol_object = Object(sym);
  33. equal("SIMD.Int32x4", SIMD.Int32x4.prototype[Symbol.toStringTag]);
  34. descriptor = Object.getOwnPropertyDescriptor(SIMD.Int32x4.prototype, Symbol.toStringTag);
  35. equal(false, descriptor.writable);
  36. equal(false, descriptor.enumerable);
  37. equal(true, descriptor.configurable);
  38. }
  39. function testToPrimitive()
  40. {
  41. ///////////////////
  42. //SIMDConstructor.prototype [ @@toPrimitive ]
  43. ///////////////////
  44. equal("function",typeof SIMD.Int32x4.prototype[Symbol.toPrimitive]);
  45. equal(1, SIMD.Int32x4.prototype[Symbol.toPrimitive].length);
  46. descriptor = Object.getOwnPropertyDescriptor(SIMD.Int32x4.prototype, Symbol.toPrimitive);
  47. equal(false, descriptor.writable);
  48. equal(false, descriptor.enumerable);
  49. equal(true, descriptor.configurable);
  50. var functionToString = SIMD.Int32x4.prototype[Symbol.toPrimitive].toString();
  51. var actualName = functionToString.substring(9, functionToString.indexOf('('));
  52. equal("[Symbol.toPrimitive]",actualName);
  53. ///////////////////////////
  54. //Overriding @@ToPrimitive
  55. ///////////////////////////
  56. SIMD.Int32x4.prototype[Symbol.toPrimitive] = undefined;
  57. var G;
  58. var functionBackup = SIMD.Int32x4.prototype.toLocaleString;
  59. SIMD.Int32x4.prototype.toLocaleString = function(){
  60. G = this;
  61. }
  62. sf0.toLocaleString();
  63. try{var x = G + G;}
  64. catch(e)
  65. {equal("Error: Number expected", e);} //@@toPrimitive is not writable
  66. //@@toPrimitive is configurable.
  67. memberBackup = Object.getOwnPropertyDescriptor(SIMD.Int32x4.prototype, Symbol.toPrimitive);
  68. delete SIMD.Int32x4.prototype[Symbol.toPrimitive];
  69. SIMD.Int32x4.prototype[Symbol.toPrimitive] = function() { return 1;}
  70. equal(2, G + G);
  71. equal(0, G - G);
  72. equal(1, G * G);
  73. equal(1, G / G);
  74. equal(true, G == G);
  75. equal(true, G === G);
  76. equal(false, G > G);
  77. equal(true, G >= G);
  78. equal(false, G < G);
  79. equal(true, G >= G);
  80. delete SIMD.Int32x4.prototype[Symbol.toPrimitive];
  81. SIMD.Int32x4.prototype[Symbol.toPrimitive] = function() { return undefined;}
  82. equal(NaN, G + G);
  83. equal(NaN, G - G);
  84. equal(NaN, G * G);
  85. equal(NaN, G / G);
  86. equal(true, G == G);
  87. equal(true, G === G);
  88. equal(false, G > G);
  89. equal(false, G >= G);
  90. equal(false, G < G);
  91. equal(false, G >= G);
  92. delete SIMD.Int32x4.prototype[Symbol.toPrimitive];
  93. SIMD.Int32x4.prototype[Symbol.toPrimitive] = function() { return "test";}
  94. equal("testtest", G + G);
  95. equal(NaN, G - G);
  96. equal(NaN, G * G);
  97. equal(NaN, G / G);
  98. equal(true, G == G);
  99. equal(true, G === G);
  100. equal(false, G > G);
  101. equal(true, G >= G);
  102. equal(false, G < G);
  103. equal(true, G >= G);
  104. delete SIMD.Int32x4.prototype[Symbol.toPrimitive];
  105. SIMD.Int32x4.prototype[Symbol.toPrimitive] = 5;
  106. try{var x = G + G;}
  107. catch(e)
  108. {equal("TypeError: The value of the property 'Symbol.toPrimitive' is not a Function object", e);}
  109. SIMD.Int32x4.prototype[Symbol.toPrimitive] = memberBackup;
  110. SIMD.Int32x4.prototype.toLocaleString = functionBackup
  111. }
  112. function testValueOf()
  113. {
  114. ///////////////////
  115. //ValueOf
  116. ///////////////////
  117. descriptor = Object.getOwnPropertyDescriptor(SIMD.Int32x4.prototype, 'valueOf');
  118. equal(true, descriptor.writable);
  119. equal(false, descriptor.enumerable);
  120. equal(true, descriptor.configurable);
  121. equal("int32x4", typeof sf0.valueOf());
  122. equal("SIMD.Int32x4(1, -2, 3, 0)",sf0.valueOf().toString());
  123. }
  124. function testToString_LocaleString()
  125. {
  126. ///////////////////
  127. //ToString / ToLocaleString
  128. ///////////////////
  129. descriptor = Object.getOwnPropertyDescriptor(SIMD.Int32x4.prototype, 'toString');
  130. equal(true, descriptor.writable);
  131. equal(false, descriptor.enumerable);
  132. equal(true, descriptor.configurable);
  133. descriptor = Object.getOwnPropertyDescriptor(SIMD.Int32x4.prototype, 'toLocaleString');
  134. equal(true, descriptor.writable);
  135. equal(false, descriptor.enumerable);
  136. equal(true, descriptor.configurable);
  137. equal("SIMD.Int32x4(1, -2, 3, 0)", sf0.toString()); //If float use ToString on lanes, the right values will be available.
  138. equal("SIMD.Int32x4(1, -2, 3, 0)", sf0.toLocaleString());
  139. equal("SIMD.Int32x4(0, 0, 0, 0)", sf3.toString());
  140. // equal("SIMD.Int32x4(0, ?, NaN, 0)", sf3.toLocaleString()); //How should this work?
  141. var functionBackup = SIMD.Int32x4.prototype.toLocaleString;
  142. var G5;
  143. SIMD.Int32x4.prototype.toLocaleString = function() {
  144. G5 = this;
  145. return "NEWTOSTRING";
  146. }
  147. equal("undefined", typeof(G5));
  148. equal("NEWTOSTRING", sf0.toLocaleString());
  149. equal("object", typeof(G5));
  150. equal("NEWTOSTRING", G5.toLocaleString());
  151. equal("NEWTOSTRING", sf0.toLocaleString());
  152. SIMD.Int32x4.prototype.toLocaleString = functionBackup;
  153. ///////////////////
  154. //PrototypeLinking toString
  155. ///////////////////
  156. functionBackup = SIMD.Int32x4.prototype.toString;
  157. SIMD.Int32x4.prototype.toString = function() {
  158. G = this;
  159. return "NEWTOSTRING";
  160. }
  161. equal("undefined", typeof(G));
  162. equal("NEWTOSTRING", sf0.toString());
  163. equal("object", typeof(G));
  164. equal("NEWTOSTRING", G.toString());
  165. equal("NEWTOSTRING", sf0.toString());
  166. SIMD.Int32x4.prototype.toString = functionBackup;
  167. }
  168. function testProtoLinking()
  169. {
  170. ///////////////////
  171. //Inheritance
  172. ///////////////////
  173. SIMD.Int32x4.prototype.myToString = function() {
  174. return "MYTOSTRING";
  175. }
  176. equal("MYTOSTRING", sf0.myToString());
  177. equal("MYTOSTRING", G.myToString());
  178. ///////////////////
  179. //PrototypeLinking valueOf
  180. ///////////////////
  181. var functionBackup = SIMD.Int32x4.prototype.valueOf;
  182. SIMD.Int32x4.prototype.valueOf = function() {
  183. G1 = this;
  184. return "NEWVALUEOF";
  185. }
  186. var G1;
  187. equal("undefined", typeof(G1));
  188. equal("NEWVALUEOF", sf0.valueOf());
  189. equal("object", typeof(G1));
  190. equal("NEWVALUEOF", G1.valueOf());
  191. equal("NEWVALUEOF", sf0.valueOf());
  192. SIMD.Int32x4.prototype.valueOf = functionBackup;
  193. }
  194. function testValueSemantics()
  195. {
  196. ///////////////////
  197. //Value semantics
  198. ///////////////////
  199. equal(true, (sf0 == sf0));
  200. equal(true, (sf0 === sf0));
  201. equal(true, (sf0 == sf1));
  202. equal(true, (sf0 === sf1));
  203. equal(false, (sf0 == sf2));
  204. equal(false, (sf0 === sf2));
  205. ///////////////////
  206. //Comparision
  207. //////////////////
  208. try{var x = sf0 > sf1;}
  209. catch(e)
  210. {equal("TypeError: SIMD type: cannot be converted to a number", e);}
  211. try{var x = sf0 < sf1;}
  212. catch(e)
  213. {equal("TypeError: SIMD type: cannot be converted to a number", e);}
  214. try{var x = sf0 >= sf1;}
  215. catch(e)
  216. {equal("TypeError: SIMD type: cannot be converted to a number", e);}
  217. try{var x = sf0 <= sf1;}
  218. catch(e)
  219. {equal("TypeError: SIMD type: cannot be converted to a number", e);}
  220. ///////////////////////
  221. //Arithmetic operators
  222. ///////////////////////
  223. equal("SIMD.Int32x4(0, 0, 0, 0)test", sf3 + "test");
  224. equal("testSIMD.Int32x4(0, 0, 0, 0)", "test" + sf3);
  225. try{var x = sf3 + sf3;}
  226. catch(e)
  227. {equal("Error: Number expected", e);} //Check
  228. try{var x = sf3 - sf3;}
  229. catch(e)
  230. {equal("Error: Number expected", e);}
  231. try{var x = sf3 / sf3;}
  232. catch(e)
  233. {equal("Error: Number expected", e);}
  234. try{var x = sf3 * sf3;}
  235. catch(e)
  236. {equal("Error: Number expected", e);}
  237. }
  238. function testUseCase1()
  239. {
  240. var f4val = SIMD.Int32x4(0, 1, 2, -3);
  241. SIMD.Int32x4.prototype.horizontalSum = function()
  242. {
  243. var value = this.valueOf();
  244. var sum = SIMD.Int32x4.extractLane(value, 0) +
  245. SIMD.Int32x4.extractLane(value, 1) +
  246. SIMD.Int32x4.extractLane(value, 2) +
  247. SIMD.Int32x4.extractLane(value, 3);
  248. return sum;
  249. }
  250. equal(0, f4val.horizontalSum());
  251. }
  252. function testWrapperObject()
  253. {
  254. testConstructor();
  255. testToStringTag();
  256. testToPrimitive();
  257. testValueOf();
  258. testToString_LocaleString();
  259. testProtoLinking();
  260. testValueSemantics();
  261. testUseCase1()
  262. }
  263. testWrapperObject();
  264. print("PASS");