1 /*
   2  * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  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 package org.graalvm.compiler.core.test;
  24 
  25 import static org.junit.Assert.assertEquals;
  26 import static org.junit.Assert.assertFalse;
  27 import static org.junit.Assert.assertTrue;
  28 
  29 import java.util.Random;
  30 
  31 import jdk.vm.ci.meta.JavaConstant;
  32 
  33 import org.junit.Test;
  34 
  35 import org.graalvm.compiler.core.common.calc.Condition;
  36 import org.graalvm.compiler.test.AddExports;
  37 
  38 @AddExports("jdk.internal.vm.ci/jdk.vm.ci.meta")
  39 public class ConditionTest {
  40 
  41     @Test
  42     public void testImplies() {
  43         Random rand = new Random(13);
  44         for (Condition c1 : Condition.values()) {
  45             for (Condition c2 : Condition.values()) {
  46                 boolean implies = c1.implies(c2);
  47                 if (implies) {
  48                     for (int i = 0; i < 1000; i++) {
  49                         JavaConstant a = JavaConstant.forInt(rand.nextInt());
  50                         JavaConstant b = JavaConstant.forInt(i < 100 ? a.asInt() : rand.nextInt());
  51                         boolean result1 = c1.foldCondition(a, b, null, false);
  52                         boolean result2 = c2.foldCondition(a, b, null, false);
  53                         if (result1) {
  54                             assertTrue(result2);
  55                         }
  56                     }
  57                 }
  58             }
  59         }
  60     }
  61 
  62     @Test
  63     public void testJoin() {
  64         Random rand = new Random(13);
  65         for (Condition c1 : Condition.values()) {
  66             for (Condition c2 : Condition.values()) {
  67                 Condition join = c1.join(c2);
  68                 assertEquals(join, c2.join(c1));
  69                 if (join != null) {
  70                     for (int i = 0; i < 1000; i++) {
  71                         JavaConstant a = JavaConstant.forInt(rand.nextInt());
  72                         JavaConstant b = JavaConstant.forInt(i < 100 ? a.asInt() : rand.nextInt());
  73                         boolean result1 = c1.foldCondition(a, b, null, false);
  74                         boolean result2 = c2.foldCondition(a, b, null, false);
  75                         boolean resultJoin = join.foldCondition(a, b, null, false);
  76                         if (result1 && result2) {
  77                             assertTrue(resultJoin);
  78                         } else {
  79                             assertFalse(resultJoin);
  80                         }
  81                     }
  82                 }
  83             }
  84         }
  85     }
  86 
  87     @Test
  88     public void testMeet() {
  89         Random rand = new Random(13);
  90         for (Condition c1 : Condition.values()) {
  91             for (Condition c2 : Condition.values()) {
  92                 Condition meet = c1.meet(c2);
  93                 assertEquals(meet, c2.meet(c1));
  94                 if (meet != null) {
  95                     for (int i = 0; i < 1000; i++) {
  96                         JavaConstant a = JavaConstant.forInt(rand.nextInt());
  97                         JavaConstant b = JavaConstant.forInt(i < 100 ? a.asInt() : rand.nextInt());
  98                         boolean result1 = c1.foldCondition(a, b, null, false);
  99                         boolean result2 = c2.foldCondition(a, b, null, false);
 100                         boolean resultMeet = meet.foldCondition(a, b, null, false);
 101                         if (result1 || result2) {
 102                             assertTrue(resultMeet);
 103                         } else {
 104                             assertFalse(resultMeet);
 105                         }
 106                     }
 107                 }
 108             }
 109         }
 110     }
 111 
 112 }