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 
  24 
  25 package org.graalvm.compiler.core.test;
  26 
  27 import static org.junit.Assert.assertEquals;
  28 import static org.junit.Assert.assertFalse;
  29 import static org.junit.Assert.assertTrue;
  30 
  31 import java.util.Random;
  32 
  33 import org.graalvm.compiler.core.common.calc.Condition;
  34 import org.junit.Test;
  35 
  36 import jdk.vm.ci.meta.JavaConstant;
  37 
  38 public class ConditionTest {
  39 
  40     @Test
  41     public void testImplies() {
  42         Random rand = new Random(13);
  43         for (Condition c1 : Condition.values()) {
  44             for (Condition c2 : Condition.values()) {
  45                 boolean implies = c1.implies(c2);
  46                 if (implies) {
  47                     for (int i = 0; i < 1000; i++) {
  48                         JavaConstant a = JavaConstant.forInt(rand.nextInt());
  49                         JavaConstant b = JavaConstant.forInt(i < 100 ? a.asInt() : rand.nextInt());
  50                         boolean result1 = c1.foldCondition(a, b, null, false);
  51                         boolean result2 = c2.foldCondition(a, b, null, false);
  52                         if (result1) {
  53                             assertTrue(result2);
  54                         }
  55                     }
  56                 }
  57             }
  58         }
  59     }
  60 
  61     @Test
  62     public void testJoin() {
  63         Random rand = new Random(13);
  64         for (Condition c1 : Condition.values()) {
  65             for (Condition c2 : Condition.values()) {
  66                 Condition join = c1.join(c2);
  67                 assertEquals(join, c2.join(c1));
  68                 if (join != null) {
  69                     for (int i = 0; i < 1000; i++) {
  70                         JavaConstant a = JavaConstant.forInt(rand.nextInt());
  71                         JavaConstant b = JavaConstant.forInt(i < 100 ? a.asInt() : rand.nextInt());
  72                         boolean result1 = c1.foldCondition(a, b, null, false);
  73                         boolean result2 = c2.foldCondition(a, b, null, false);
  74                         boolean resultJoin = join.foldCondition(a, b, null, false);
  75                         if (result1 && result2) {
  76                             assertTrue(resultJoin);
  77                         } else {
  78                             assertFalse(resultJoin);
  79                         }
  80                     }
  81                 }
  82             }
  83         }
  84     }
  85 
  86     @Test
  87     public void testMeet() {
  88         Random rand = new Random(13);
  89         for (Condition c1 : Condition.values()) {
  90             for (Condition c2 : Condition.values()) {
  91                 Condition meet = c1.meet(c2);
  92                 assertEquals(meet, c2.meet(c1));
  93                 if (meet != null) {
  94                     for (int i = 0; i < 1000; i++) {
  95                         JavaConstant a = JavaConstant.forInt(rand.nextInt());
  96                         JavaConstant b = JavaConstant.forInt(i < 100 ? a.asInt() : rand.nextInt());
  97                         boolean result1 = c1.foldCondition(a, b, null, false);
  98                         boolean result2 = c2.foldCondition(a, b, null, false);
  99                         boolean resultMeet = meet.foldCondition(a, b, null, false);
 100                         if (result1 || result2) {
 101                             assertTrue(resultMeet);
 102                         } else {
 103                             assertFalse(resultMeet);
 104                         }
 105                     }
 106                 }
 107             }
 108         }
 109     }
 110 
 111 }