4444import static org .junit .Assert .assertFalse ;
4545import static org .junit .Assert .assertSame ;
4646import static org .junit .Assert .assertTrue ;
47+ import static org .junit .Assert .fail ;
4748
4849import java .math .BigInteger ;
4950
8182import com .oracle .truffle .js .runtime .builtins .JSSet ;
8283import com .oracle .truffle .js .runtime .builtins .JSString ;
8384import com .oracle .truffle .js .runtime .builtins .JSUserObject ;
85+ import com .oracle .truffle .js .runtime .objects .JSLazyString ;
8486import com .oracle .truffle .js .runtime .objects .Null ;
8587import com .oracle .truffle .js .runtime .objects .Undefined ;
8688import com .oracle .truffle .js .test .JSTest ;
8789import com .oracle .truffle .js .test .polyglot .ForeignTestMap ;
8890
8991public class JSRuntimeTest extends JSTest {
9092
93+ private static final double BIGDELTA = 0.00001 ;
94+
9195 @ Override
9296 public void setup () {
9397 super .setup ();
@@ -110,6 +114,20 @@ public void testEqual() {
110114 assertFalse (JSRuntime .equal (true , Undefined .instance ));
111115 assertFalse (JSRuntime .equal (Undefined .instance , 1 ));
112116 assertTrue (JSRuntime .equal (Float .MAX_VALUE , Float .MAX_VALUE ));
117+
118+ DynamicObject obj = JSUserObject .create (context );
119+ assertFalse (JSRuntime .equal (obj , Null .instance ));
120+ assertFalse (JSRuntime .equal (obj , Undefined .instance ));
121+ assertFalse (JSRuntime .equal (Null .instance , obj ));
122+ assertFalse (JSRuntime .equal (Undefined .instance , obj ));
123+ assertTrue (JSRuntime .equal (obj , obj ));
124+ assertFalse (JSRuntime .equal (obj , JSUserObject .create (context )));
125+
126+ BigInt bi1a = new BigInt (new BigInteger ("0123456789" ));
127+ BigInt bi1b = new BigInt (new BigInteger ("0123456789" ));
128+ BigInt bi2 = new BigInt (new BigInteger ("9876543210" ));
129+ assertTrue (JSRuntime .equal (bi1a , bi1b ));
130+ assertFalse (JSRuntime .equal (bi1a , bi2 ));
113131 }
114132
115133 @ Test
@@ -133,6 +151,8 @@ public void testQuote() {
133151
134152 @ Test
135153 public void testImportValue () {
154+ testHelper .getJSContext (); // initialize JSContext
155+
136156 assertEquals (Null .instance , JSRuntime .importValue (null ));
137157
138158 assertEquals (42 , JSRuntime .importValue (42 ));
@@ -143,6 +163,17 @@ public void testImportValue() {
143163 // same for now, might not hold eternally
144164 assertSame (42 , JSRuntime .importValue ((byte ) 42 ));
145165 assertSame (42 , JSRuntime .importValue ((short ) 42 ));
166+
167+ assertSame (42 , JSRuntime .importValue (42L ));
168+ assertEquals (BigInt .valueOf (Long .MAX_VALUE ), JSRuntime .importValue (Long .MAX_VALUE ));
169+ assertEquals (42.0 , (double ) JSRuntime .importValue ((float ) 42 ), BIGDELTA );
170+
171+ try {
172+ JSRuntime .importValue (new Object ());
173+ fail ();
174+ } catch (Exception ex ) {
175+ assertTrue (ex .getMessage ().contains ("not supported in JavaScript" ));
176+ }
146177 }
147178
148179 @ Test
@@ -339,4 +370,167 @@ public void testNodeToString() {
339370 assertTrue (str .contains ("DualNode" ));
340371 assertTrue (str .contains (":program" ));
341372 }
373+
374+ @ Test
375+ public void testToLength () {
376+ // toLength(double)
377+ assertTrue (JSRuntime .toLength (-3.14 ) == 0 );
378+ assertTrue (JSRuntime .toLength (JSRuntime .MAX_SAFE_INTEGER * 2 ) == JSRuntime .MAX_SAFE_INTEGER );
379+ assertTrue (JSRuntime .toLength (Math .PI ) == Math .PI );
380+
381+ // toLength(int)
382+ assertTrue (JSRuntime .toLength (-3 ) == 0 );
383+ assertTrue (JSRuntime .toLength (42 ) == 42 );
384+ }
385+
386+ @ Test
387+ public void testLength () {
388+ testHelper .getJSContext (); // intializes Context
389+
390+ // lengthIntl(CharSequence)
391+ assertEquals (3 , JSRuntime .length ("ABC" ));
392+ assertEquals (42 , JSRuntime .length (new TestCharSequence ()));
393+ assertEquals (40 , JSRuntime .length (createLazyString ()));
394+ }
395+
396+ private static CharSequence createLazyString () {
397+ return JSLazyString .create ("01234567890123456789" , "01234567890123456789" );
398+ }
399+
400+ @ Test
401+ public void testCharAt () {
402+ testHelper .getJSContext (); // initializes Context
403+
404+ // charAt(CharSequence, int)
405+ assertEquals ('A' , JSRuntime .charAt ("ABC" , 0 ));
406+ assertEquals ('A' , JSRuntime .charAt (new TestCharSequence (), 0 ));
407+ assertEquals ('0' , JSRuntime .charAt (createLazyString (), 0 ));
408+ }
409+
410+ private static class TestCharSequence implements CharSequence {
411+ @ Override
412+ public int length () {
413+ return 42 ;
414+ }
415+
416+ @ Override
417+ public char charAt (int index ) {
418+ return 'A' ;
419+ }
420+
421+ @ Override
422+ public CharSequence subSequence (int start , int end ) {
423+ return null ;
424+ }
425+ }
426+
427+ @ Test
428+ public void testToUInt8 () {
429+ // toUInt8(Object)
430+ assertTrue (JSRuntime .toUInt8 ((Object ) 3 ) == 3 );
431+ assertTrue (JSRuntime .toUInt8 ((Object ) 3.14 ) == 3 );
432+ assertTrue (JSRuntime .toUInt8 ((Object ) Double .POSITIVE_INFINITY ) == 0 );
433+ }
434+
435+ @ Test
436+ public void testToInt8 () {
437+ // toInt8(Object)
438+ assertTrue (JSRuntime .toInt8 ((Object ) 3 ) == 3 );
439+ assertTrue (JSRuntime .toInt8 ((Object ) 3.14 ) == 3 );
440+ assertTrue (JSRuntime .toInt8 ((Object ) Double .POSITIVE_INFINITY ) == 0 );
441+ }
442+
443+ @ Test
444+ public void testToUInt16 () {
445+ // toUInt16(Object)
446+ assertTrue (JSRuntime .toUInt16 ((Object ) 3 ) == 3 );
447+ assertTrue (JSRuntime .toUInt16 ((Object ) 3.14 ) == 3 );
448+ assertTrue (JSRuntime .toUInt16 ((Object ) Double .POSITIVE_INFINITY ) == 0 );
449+ }
450+
451+ @ Test
452+ public void testToInt16 () {
453+ // toInt16(Object)
454+ assertTrue (JSRuntime .toInt16 ((Object ) 3 ) == 3 );
455+ assertTrue (JSRuntime .toInt16 ((Object ) 3.14 ) == 3 );
456+ assertTrue (JSRuntime .toInt16 ((Object ) Double .POSITIVE_INFINITY ) == 0 );
457+ }
458+
459+ @ Test
460+ public void testToInt32 () {
461+ // toInt32(Object)
462+ assertTrue (JSRuntime .toInt32 ((Object ) 3 ) == 3 );
463+ assertTrue (JSRuntime .toInt32 ((Object ) 3.14 ) == 3 );
464+ assertTrue (JSRuntime .toInt32 ((Object ) 3L ) == 3 );
465+ assertTrue (JSRuntime .toInt32 ((Object ) Double .POSITIVE_INFINITY ) == 0 );
466+ }
467+
468+ @ Test
469+ public void testToObject () {
470+ JSContext ctx = testHelper .getJSContext ();
471+
472+ // toObjectFromPrimitive(Object)
473+ assertTrue (JSRuntime .toObject (ctx , true ) != null );
474+ assertTrue (JSRuntime .toObject (ctx , "String" ) != null );
475+ assertTrue (JSRuntime .toObject (ctx , (float ) 3.14 ) != null );
476+ }
477+
478+ @ Test
479+ public void testToStringIsString () {
480+ testHelper .getJSContext (); // initialize JSContext
481+
482+ // toStringIsString(Object)
483+ assertEquals ("ABC" , JSRuntime .toStringIsString ("ABC" ));
484+ assertEquals (40 , JSRuntime .toStringIsString (createLazyString ()).length ());
485+ }
486+
487+ @ Test
488+ public void testTrimJSWhiteSpace () {
489+ // trimJSWhiteSpace(String)
490+ assertEquals ("A" , JSRuntime .trimJSWhiteSpace (" A " ));
491+ assertEquals ("A" , JSRuntime .trimJSWhiteSpace ("A " ));
492+ assertEquals ("A" , JSRuntime .trimJSWhiteSpace (" A" ));
493+ assertEquals ("A" , JSRuntime .trimJSWhiteSpace ("A" ));
494+ assertEquals ("A" , JSRuntime .trimJSWhiteSpace (" A " ));
495+ assertEquals ("AB" , JSRuntime .trimJSWhiteSpace ("AB " ));
496+ assertEquals ("AB" , JSRuntime .trimJSWhiteSpace (" AB" ));
497+ assertEquals ("AB" , JSRuntime .trimJSWhiteSpace ("AB" ));
498+ assertEquals ("" , JSRuntime .trimJSWhiteSpace (" " ));
499+ assertEquals ("" , JSRuntime .trimJSWhiteSpace ("" ));
500+ }
501+
502+ @ Test
503+ public void testIntValue () {
504+ // intValue(Number)
505+ assertEquals (42 , JSRuntime .intValue (42 ));
506+ assertEquals (42 , JSRuntime .intValue (42.3 ));
507+ assertEquals (42 , JSRuntime .intValue (42L ));
508+ }
509+
510+ @ Test
511+ public void testFloatValue () {
512+ // floatValue(Number)
513+ assertEquals (42 , JSRuntime .floatValue (42 ), BIGDELTA );
514+ assertEquals (42.3 , JSRuntime .floatValue (42.3 ), BIGDELTA );
515+ assertEquals (42 , JSRuntime .floatValue (42L ), BIGDELTA );
516+ }
517+
518+ @ Test
519+ public void testMathCeil () {
520+ // mathCeil(double)
521+ assertTrue (Double .isNaN (JSRuntime .mathCeil (Double .NaN )));
522+ assertTrue (JSRuntime .isNegativeZero (JSRuntime .mathCeil (-0.0 )));
523+ }
524+
525+ @ Test
526+ public void testExportValue () {
527+ testHelper .getJSContext (); // initialize JSContext
528+
529+ // exportValue(Object)
530+ assertEquals (42.0 , (double ) JSRuntime .exportValue (SafeInteger .valueOf (42 )), BIGDELTA );
531+
532+ Object exportedLazyString = JSRuntime .exportValue (createLazyString ());
533+ assertTrue (exportedLazyString instanceof String );
534+ assertEquals (createLazyString ().toString (), exportedLazyString );
535+ }
342536}
0 commit comments