< prev index next >

test/jdk/java/util/Optional/BasicInt.java

Print this page




  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 8195649
  26  * @summary Basic functional test of OptionalInt
  27  * @author Mike Duigou
  28  * @build ObscureException
  29  * @run testng BasicInt
  30  */
  31 
  32 import java.util.NoSuchElementException;
  33 import java.util.OptionalInt;
  34 import java.util.concurrent.atomic.AtomicBoolean;


  35 
  36 import static org.testng.Assert.*;
  37 import org.testng.annotations.Test;
  38 
  39 public class BasicInt {
  40 
  41     static final int INTVAL = 33_550_336;
  42     static final int UNEXPECTED = 0xCAFEBABE;
  43 
  44     /**
  45      * Checks a block of assertions over an empty OptionalInt.
  46      */
  47     void checkEmpty(OptionalInt empty) {
  48         assertTrue(empty.equals(OptionalInt.empty()));
  49         assertTrue(OptionalInt.empty().equals(empty));
  50         assertFalse(empty.equals(OptionalInt.of(UNEXPECTED)));
  51         assertFalse(OptionalInt.of(UNEXPECTED).equals(empty));
  52         assertFalse(empty.equals("unexpected"));
  53 
  54         assertFalse(empty.isPresent());

  55         assertEquals(empty.hashCode(), 0);
  56         assertEquals(empty.orElse(UNEXPECTED), UNEXPECTED);
  57         assertEquals(empty.orElseGet(() -> UNEXPECTED), UNEXPECTED);
  58 
  59         assertThrows(NoSuchElementException.class, () -> empty.getAsInt());
  60         assertThrows(NoSuchElementException.class, () -> empty.orElseThrow());
  61         assertThrows(ObscureException.class,       () -> empty.orElseThrow(ObscureException::new));
  62 
  63         var b = new AtomicBoolean();
  64         empty.ifPresent(s -> b.set(true));
  65         assertFalse(b.get());
  66 
  67         var b1 = new AtomicBoolean(false);
  68         var b2 = new AtomicBoolean(false);
  69         empty.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));
  70         assertFalse(b1.get());
  71         assertTrue(b2.get());
  72 
  73         assertEquals(empty.toString(), "OptionalInt.empty");
  74     }
  75 
  76     /**
  77      * Checks a block of assertions over an OptionalInt that is expected to
  78      * have a particular value present.
  79      */
  80     void checkPresent(OptionalInt opt, int expected) {
  81         assertFalse(opt.equals(OptionalInt.empty()));
  82         assertFalse(OptionalInt.empty().equals(opt));
  83         assertTrue(opt.equals(OptionalInt.of(expected)));
  84         assertTrue(OptionalInt.of(expected).equals(opt));
  85         assertFalse(opt.equals(OptionalInt.of(UNEXPECTED)));
  86         assertFalse(OptionalInt.of(UNEXPECTED).equals(opt));
  87         assertFalse(opt.equals("unexpected"));
  88 
  89         assertTrue(opt.isPresent());

  90         assertEquals(opt.hashCode(), Integer.hashCode(expected));
  91         assertEquals(opt.orElse(UNEXPECTED), expected);
  92         assertEquals(opt.orElseGet(() -> UNEXPECTED), expected);
  93 
  94         assertEquals(opt.getAsInt(), expected);
  95         assertEquals(opt.orElseThrow(), expected);
  96         assertEquals(opt.orElseThrow(ObscureException::new), expected);
  97 
  98         var b = new AtomicBoolean(false);
  99         opt.ifPresent(s -> b.set(true));
 100         assertTrue(b.get());
 101 
 102         var b1 = new AtomicBoolean(false);
 103         var b2 = new AtomicBoolean(false);
 104         opt.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));
 105         assertTrue(b1.get());
 106         assertFalse(b2.get());
 107 
 108         assertEquals(opt.toString(), "OptionalInt[" + expected + "]");
 109     }




  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 8195649
  26  * @summary Basic functional test of OptionalInt
  27  * @author Mike Duigou
  28  * @build ObscureException
  29  * @run testng BasicInt
  30  */
  31 
  32 import java.util.NoSuchElementException;
  33 import java.util.OptionalInt;
  34 import java.util.concurrent.atomic.AtomicBoolean;
  35 import java.util.function.IntPredicate;
  36 import java.util.List;
  37 
  38 import static org.testng.Assert.*;
  39 import org.testng.annotations.Test;
  40 
  41 public class BasicInt {
  42 
  43     static final int INTVAL = 33_550_336;
  44     static final int UNEXPECTED = 0xCAFEBABE;
  45 
  46     /**
  47      * Checks a block of assertions over an empty OptionalInt.
  48      */
  49     void checkEmpty(OptionalInt empty) {
  50         assertTrue(empty.equals(OptionalInt.empty()));
  51         assertTrue(OptionalInt.empty().equals(empty));
  52         assertFalse(empty.equals(OptionalInt.of(UNEXPECTED)));
  53         assertFalse(OptionalInt.of(UNEXPECTED).equals(empty));
  54         assertFalse(empty.equals("unexpected"));
  55 
  56         assertFalse(empty.isPresent());
  57         assertTrue(empty.isEmpty());
  58         assertEquals(empty.hashCode(), 0);
  59         assertEquals(empty.orElse(UNEXPECTED), UNEXPECTED);
  60         assertEquals(empty.orElseGet(() -> UNEXPECTED), UNEXPECTED);
  61 
  62         assertThrows(NoSuchElementException.class, () -> empty.getAsInt());
  63         assertThrows(NoSuchElementException.class, () -> empty.orElseThrow());
  64         assertThrows(ObscureException.class,       () -> empty.orElseThrow(ObscureException::new));
  65 
  66         var b = new AtomicBoolean();
  67         empty.ifPresent(s -> b.set(true));
  68         assertFalse(b.get());
  69 
  70         var b1 = new AtomicBoolean(false);
  71         var b2 = new AtomicBoolean(false);
  72         empty.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));
  73         assertFalse(b1.get());
  74         assertTrue(b2.get());
  75 
  76         assertEquals(empty.toString(), "OptionalInt.empty");
  77     }
  78 
  79     /**
  80      * Checks a block of assertions over an OptionalInt that is expected to
  81      * have a particular value present.
  82      */
  83     void checkPresent(OptionalInt opt, int expected) {
  84         assertFalse(opt.equals(OptionalInt.empty()));
  85         assertFalse(OptionalInt.empty().equals(opt));
  86         assertTrue(opt.equals(OptionalInt.of(expected)));
  87         assertTrue(OptionalInt.of(expected).equals(opt));
  88         assertFalse(opt.equals(OptionalInt.of(UNEXPECTED)));
  89         assertFalse(OptionalInt.of(UNEXPECTED).equals(opt));
  90         assertFalse(opt.equals("unexpected"));
  91 
  92         assertTrue(opt.isPresent());
  93         assertFalse(opt.isEmpty());
  94         assertEquals(opt.hashCode(), Integer.hashCode(expected));
  95         assertEquals(opt.orElse(UNEXPECTED), expected);
  96         assertEquals(opt.orElseGet(() -> UNEXPECTED), expected);
  97 
  98         assertEquals(opt.getAsInt(), expected);
  99         assertEquals(opt.orElseThrow(), expected);
 100         assertEquals(opt.orElseThrow(ObscureException::new), expected);
 101 
 102         var b = new AtomicBoolean(false);
 103         opt.ifPresent(s -> b.set(true));
 104         assertTrue(b.get());
 105 
 106         var b1 = new AtomicBoolean(false);
 107         var b2 = new AtomicBoolean(false);
 108         opt.ifPresentOrElse(s -> b1.set(true), () -> b2.set(true));
 109         assertTrue(b1.get());
 110         assertFalse(b2.get());
 111 
 112         assertEquals(opt.toString(), "OptionalInt[" + expected + "]");
 113     }


< prev index next >