testBool32x4.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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.Bool32x4(1.35, -2.0, 3.4, 0.0);
  7. var sf1 = SIMD.Bool32x4(1.35, -2.0, 3.4, 0.0);
  8. var sf2 = SIMD.Bool32x4(1.35, -2.0, 3.14, 0.0);
  9. var sf3 = SIMD.Bool32x4(0, Infinity, NaN, -0);
  10. function testConstructor()
  11. {
  12. ////////////////////
  13. //Constructor
  14. ///////////////////
  15. equal("object", typeof SIMD.Bool32x4.prototype);
  16. equal("function", typeof SIMD.Bool32x4.prototype.constructor);
  17. equal("function", typeof SIMD.Bool32x4.prototype.toString);
  18. equal("function", typeof SIMD.Bool32x4.prototype.toLocaleString);
  19. equal("function", typeof SIMD.Bool32x4.prototype.valueOf);
  20. equal(SIMD.Bool32x4, SIMD.Bool32x4.prototype.constructor);
  21. descriptor = Object.getOwnPropertyDescriptor(SIMD.Bool32x4, '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.Bool32x4", SIMD.Bool32x4.prototype[Symbol.toStringTag]);
  34. descriptor = Object.getOwnPropertyDescriptor(SIMD.Bool32x4.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.Bool32x4.prototype[Symbol.toPrimitive]);
  45. equal(1, SIMD.Bool32x4.prototype[Symbol.toPrimitive].length);
  46. descriptor = Object.getOwnPropertyDescriptor(SIMD.Bool32x4.prototype, Symbol.toPrimitive);
  47. equal(false, descriptor.writable);
  48. equal(false, descriptor.enumerable);
  49. equal(true, descriptor.configurable);
  50. var functionToString = SIMD.Bool32x4.prototype[Symbol.toPrimitive].toString();
  51. var actualName = functionToString.substring(9, functionToString.indexOf('('));
  52. equal("[Symbol.toPrimitive]",actualName);
  53. ///////////////////////////
  54. //Overriding @@ToPrimitive
  55. ///////////////////////////
  56. SIMD.Bool32x4.prototype[Symbol.toPrimitive] = undefined;
  57. var G;
  58. var functionBackup = SIMD.Bool32x4.prototype.toLocaleString;
  59. SIMD.Bool32x4.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.Bool32x4.prototype, Symbol.toPrimitive);
  68. delete SIMD.Bool32x4.prototype[Symbol.toPrimitive];
  69. SIMD.Bool32x4.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.Bool32x4.prototype[Symbol.toPrimitive];
  81. SIMD.Bool32x4.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.Bool32x4.prototype[Symbol.toPrimitive];
  93. SIMD.Bool32x4.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.Bool32x4.prototype[Symbol.toPrimitive];
  105. SIMD.Bool32x4.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.Bool32x4.prototype[Symbol.toPrimitive] = memberBackup;
  110. SIMD.Bool32x4.prototype.toLocaleString = functionBackup
  111. }
  112. function testValueOf()
  113. {
  114. ///////////////////
  115. //ValueOf
  116. ///////////////////
  117. descriptor = Object.getOwnPropertyDescriptor(SIMD.Bool32x4.prototype, 'valueOf');
  118. equal(true, descriptor.writable);
  119. equal(false, descriptor.enumerable);
  120. equal(true, descriptor.configurable);
  121. equal("bool32x4", typeof sf0.valueOf());
  122. equal("SIMD.Bool32x4(true, true, true, false)", sf0.toString());
  123. }
  124. function testToString_LocaleString()
  125. {
  126. ///////////////////
  127. //ToString / ToLocaleString
  128. ///////////////////
  129. descriptor = Object.getOwnPropertyDescriptor(SIMD.Bool32x4.prototype, 'toString');
  130. equal(true, descriptor.writable);
  131. equal(false, descriptor.enumerable);
  132. equal(true, descriptor.configurable);
  133. descriptor = Object.getOwnPropertyDescriptor(SIMD.Bool32x4.prototype, 'toLocaleString');
  134. equal(true, descriptor.writable);
  135. equal(false, descriptor.enumerable);
  136. equal(true, descriptor.configurable);
  137. equal("SIMD.Bool32x4(true, true, true, false)", sf0.toString()); //If float use ToString on lanes, the right values will be available.
  138. equal("SIMD.Bool32x4(true, true, true, false)", sf0.toLocaleString());
  139. equal("SIMD.Bool32x4(false, true, false, false)", sf3.toString());
  140. // equal("SIMD.Bool32x4(0, ?, NaN, 0)", sf3.toLocaleString()); //How should this work?
  141. var origFn = SIMD.Bool32x4.prototype.toLocaleString;
  142. var G5;
  143. SIMD.Bool32x4.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.Bool32x4.prototype.toLocaleString = origFn;
  153. ///////////////////
  154. //PrototypeLinking toString
  155. ///////////////////
  156. var origFn = SIMD.Bool32x4.prototype.toString;
  157. SIMD.Bool32x4.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.Bool32x4.prototype.toString = origFn;
  167. }
  168. function testProtoLinking()
  169. {
  170. ///////////////////
  171. //Inheritance
  172. ///////////////////
  173. SIMD.Bool32x4.prototype.myToString = function() {
  174. return "MYTOSTRING";
  175. }
  176. equal("MYTOSTRING", sf0.myToString());
  177. equal("MYTOSTRING", G.myToString());
  178. ///////////////////
  179. //PrototypeLinking valueOf
  180. ///////////////////
  181. SIMD.Bool32x4.prototype.valueOf = function() {
  182. G1 = this;
  183. return "NEWVALUEOF";
  184. }
  185. var G1;
  186. equal("undefined", typeof(G1));
  187. equal("NEWVALUEOF", sf0.valueOf());
  188. equal("object", typeof(G1));
  189. equal("NEWVALUEOF", G1.valueOf());
  190. equal("NEWVALUEOF", sf0.valueOf());
  191. }
  192. function testValueSemantics()
  193. {
  194. ///////////////////
  195. //Value semantics
  196. ///////////////////
  197. equal(true, (sf0 == sf0));
  198. equal(true, (sf0 === sf0));
  199. equal(true, (sf0 == sf1));
  200. equal(true, (sf0 === sf1));
  201. equal(true, (sf0 == sf2));
  202. equal(true, (sf0 === sf2));
  203. ///////////////////
  204. //Comparision
  205. //////////////////
  206. try{var x = sf0 > sf1;}
  207. catch(e)
  208. {equal("TypeError: SIMD type: cannot be converted to a number", e);}
  209. try{var x = sf0 < sf1;}
  210. catch(e)
  211. {equal("TypeError: SIMD type: cannot be converted to a number", e);}
  212. try{var x = sf0 >= sf1;}
  213. catch(e)
  214. {equal("TypeError: SIMD type: cannot be converted to a number", e);}
  215. try{var x = sf0 <= sf1;}
  216. catch(e)
  217. {equal("TypeError: SIMD type: cannot be converted to a number", e);}
  218. ///////////////////////
  219. //Arithmetic operators
  220. ///////////////////////
  221. equal("SIMD.Bool32x4(false, true, false, false)test", sf3 + "test");
  222. equal("testSIMD.Bool32x4(false, true, false, false)", "test" + sf3);
  223. try{var x = sf3 + sf3;}
  224. catch(e)
  225. {equal("Error: Number expected", e);} //Check
  226. try{var x = sf3 - sf3;}
  227. catch(e)
  228. {equal("Error: Number expected", e);}
  229. try{var x = sf3 / sf3;}
  230. catch(e)
  231. {equal("Error: Number expected", e);}
  232. try{var x = sf3 * sf3;}
  233. catch(e)
  234. {equal("Error: Number expected", e);}
  235. }
  236. //The following test can be enabled when extract lane builtin for bools are merged.
  237. // function testHorizontalOR()
  238. // {
  239. // var b1 = SIMD.Bool32x4(true, false, false, true);
  240. //
  241. // SIMD.Bool32x4.prototype.horizontalOR = function()
  242. // {
  243. // var b4 = this.valueOf();
  244. // return (SIMD.Bool32x4.extractLane(b4, 0) ||
  245. // SIMD.Bool32x4.extractLane(b4, 1) ||
  246. // SIMD.Bool32x4.extractLane(b4, 2) ||
  247. // SIMD.Bool32x4.extractLane(b4, 3) );
  248. // }
  249. //
  250. // equal(true, b1.horizontalOR());
  251. // }
  252. function testWrapperObject()
  253. {
  254. testConstructor();
  255. testToStringTag();
  256. testToPrimitive();
  257. testValueOf();
  258. testToString_LocaleString();
  259. testProtoLinking();
  260. testValueSemantics();
  261. // testHorizontalOR()
  262. }
  263. testWrapperObject();
  264. print("PASS");