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