Skip to content

Commit d2b501b

Browse files
committed
Add tests for PutNode.executeIfAbsent and executeWithFlagsIfPresent.
1 parent 8a41b5a commit d2b501b

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

truffle/src/com.oracle.truffle.api.object.test/src/com/oracle/truffle/api/object/test/DynamicObjectNodesTest.java

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,16 @@ public void testPutIfPresent() {
377377
assertTrue(setNode.executeIfPresent(o2, key1, intval1));
378378
assertEquals(intval1, uncachedGet(o2, key1));
379379
assertSame(o1.getShape(), o2.getShape());
380+
assertTrue(setNode.executeWithFlagsIfPresent(o2, key1, intval2, 0b0));
381+
assertEquals(intval2, uncachedGet(o2, key1));
382+
assertSame(o1.getShape(), o2.getShape());
383+
384+
assertTrue(setNode.executeWithFlagsIfPresent(o2, key1, intval1, 0b11));
385+
assertEquals(intval1, uncachedGet(o2, key1));
386+
assertEquals(0b11, uncachedGetPropertyFlags(o2, key1, -1));
387+
assertTrue(setNode.executeWithFlagsIfPresent(o2, key1, intval2, 0b0));
388+
assertEquals(intval2, uncachedGet(o2, key1));
389+
assertEquals(0b0, uncachedGetPropertyFlags(o2, key1, -1));
380390

381391
String strval1 = "asdf";
382392
setNode.execute(o1, key1, strval1);
@@ -388,19 +398,58 @@ public void testPutIfPresent() {
388398
var containsKeyNode2 = createContainsKeyNode();
389399
assertFalse(DynamicObject.ContainsKeyNode.getUncached().execute(o1, key2));
390400
assertFalse(setNode2.executeIfPresent(o1, key2, strval2));
391-
// assertTrue(setNode2.accepts(o1));
401+
assertFalse(setNode2.executeWithFlagsIfPresent(o1, key2, strval2, 0b11));
392402
assertFalse(containsKeyNode2.execute(o1, key2));
393403
assertEquals(null, uncachedGet(o1, key2));
394404

395405
setNode2.execute(o1, key2, strval2);
396406
assertTrue(DynamicObject.ContainsKeyNode.getUncached().execute(o1, key2));
397407
assertEquals(strval2, uncachedGet(o1, key2));
408+
assertEquals(0, uncachedGetPropertyFlags(o1, key2, -1));
409+
setNode2.executeWithFlags(o2, key2, strval2, 0b11);
410+
assertTrue(DynamicObject.ContainsKeyNode.getUncached().execute(o2, key2));
411+
assertEquals(strval2, uncachedGet(o2, key2));
412+
assertEquals(0b11, uncachedGetPropertyFlags(o2, key2, -1));
398413

399414
var setNode3 = createPutNode();
400415
assertTrue(setNode3.executeIfPresent(o1, key2, intval1));
401416
assertEquals(intval1, uncachedGet(o1, key2));
402417
}
403418

419+
@Test
420+
public void testPutIfAbsent() {
421+
String key1 = "key1";
422+
String key2 = "key2";
423+
int intval1 = 42;
424+
int intval2 = 43;
425+
DynamicObject o1 = createEmpty();
426+
DynamicObject o2 = createEmpty();
427+
assertSame(o1.getShape(), o2.getShape());
428+
429+
var setNode = createPutNode();
430+
assertTrue(setNode.executeIfAbsent(o1, key1, intval1));
431+
assertTrue(setNode.executeWithFlagsIfAbsent(o2, key1, intval1, 0));
432+
assertSame(o1.getShape(), o2.getShape());
433+
assertEquals(intval1, uncachedGet(o1, key1));
434+
assertEquals(intval1, uncachedGet(o2, key1));
435+
assertEquals(0, uncachedGetPropertyFlags(o1, key1, -1));
436+
assertEquals(0, uncachedGetPropertyFlags(o2, key1, -1));
437+
438+
Shape shapeBefore = o1.getShape();
439+
assertFalse(setNode.executeIfAbsent(o1, key1, intval2));
440+
assertFalse(setNode.executeWithFlagsIfAbsent(o1, key1, intval2, 0b11));
441+
assertEquals(intval1, uncachedGet(o1, key1));
442+
Shape shapeAfter = o1.getShape();
443+
assertSame(shapeBefore, shapeAfter);
444+
445+
assertTrue(setNode.executeWithFlagsIfAbsent(o1, key2, intval2, 0b11));
446+
assertTrue(setNode.executeIfAbsent(o2, key2, intval2));
447+
assertEquals(intval2, uncachedGet(o1, key2));
448+
assertEquals(intval2, uncachedGet(o2, key2));
449+
assertEquals(0b11, uncachedGetPropertyFlags(o1, key2, -1));
450+
assertEquals(0, uncachedGetPropertyFlags(o2, key2, -1));
451+
}
452+
404453
@Test
405454
public void testPut2() {
406455
DynamicObject o1 = createEmpty();
@@ -1022,6 +1071,10 @@ private static Property uncachedGetProperty(DynamicObject obj, Object key) {
10221071
return DynamicObject.GetPropertyNode.getUncached().execute(obj, key);
10231072
}
10241073

1074+
private static int uncachedGetPropertyFlags(DynamicObject obj, Object key, int defaultValue) {
1075+
return DynamicObject.GetPropertyFlagsNode.getUncached().execute(obj, key, defaultValue);
1076+
}
1077+
10251078
private static Object newObjectType() {
10261079
return new Object() {
10271080
};

0 commit comments

Comments
 (0)