test-ng/tests/org/openjdk/tests/java/util/functions/IntUnaryOperatorTest.java

Print this page
rev 5834 : Rename compareAndSet methods, add updateAndGet/getAndUpdate to AtomicReference, add tests
Reviewed-by: briangoetz,smarks,mduigo
Contributed-by: Jim Gish <jim.gish@oracle.com>


  40     }
  41 
  42     /**
  43      * Test of operate method, of class IntUnaryOperator.
  44      */
  45     public void testOperate() {
  46         Integer operand = 3;
  47         IntUnaryOperator instance = new IncByTwo();
  48         int expResult = 5;
  49         int result = instance.operate(operand);
  50         assertEquals(result, expResult);
  51     }
  52 
  53     public void testOperandInvariance() {
  54         Integer operand = 7;
  55         IntUnaryOperator op = new IncByTwo();
  56         assertEquals( op.operate(operand), op.operate(operand));
  57         assertEquals( operand.intValue(), 7 );
  58     }
  59     
  60     public void tryItWithLambdaExpression() {
  61         AtomicInteger atomInt = new AtomicInteger(3);
  62         assertEquals( atomInt.compareAndSet(x -> x + 1), 4 );





  63     };
  64     
  65     public class IncByTwo implements IntUnaryOperator {
  66 
  67         @Override
  68         public int operate(int operand) {
  69             return operand+2;
  70         }
  71     }
  72 }


  40     }
  41 
  42     /**
  43      * Test of operate method, of class IntUnaryOperator.
  44      */
  45     public void testOperate() {
  46         Integer operand = 3;
  47         IntUnaryOperator instance = new IncByTwo();
  48         int expResult = 5;
  49         int result = instance.operate(operand);
  50         assertEquals(result, expResult);
  51     }
  52 
  53     public void testOperandInvariance() {
  54         Integer operand = 7;
  55         IntUnaryOperator op = new IncByTwo();
  56         assertEquals( op.operate(operand), op.operate(operand));
  57         assertEquals( operand.intValue(), 7 );
  58     }
  59     
  60     public void testUpdateOps() {
  61         AtomicInteger atomInt = new AtomicInteger(3);
  62         // make sure we the updated value is returned
  63         assertEquals( atomInt.updateAndGet(x -> x + 1), 4 );
  64         // then the previous value is returned
  65         assertEquals( atomInt.getAndUpdate(x -> x + 1), 4 );
  66         // finally, verify that the update was successful
  67         assertEquals( atomInt.get(), 5 );
  68     };
  69     
  70     public class IncByTwo implements IntUnaryOperator {
  71 
  72         @Override
  73         public int operate(int operand) {
  74             return operand+2;
  75         }
  76     }
  77 }