001/*
002 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.compiler.test;
024
025import static org.junit.Assert.*;
026
027import java.util.*;
028
029import jdk.internal.jvmci.meta.*;
030
031import org.junit.*;
032
033import com.oracle.graal.compiler.common.calc.*;
034
035public class ConditionTest {
036
037    @Test
038    public void testImplies() {
039        Random rand = new Random(13);
040        for (Condition c1 : Condition.values()) {
041            for (Condition c2 : Condition.values()) {
042                boolean implies = c1.implies(c2);
043                if (implies) {
044                    for (int i = 0; i < 1000; i++) {
045                        JavaConstant a = JavaConstant.forInt(rand.nextInt());
046                        JavaConstant b = JavaConstant.forInt(i < 100 ? a.asInt() : rand.nextInt());
047                        boolean result1 = c1.foldCondition(a, b, null, false);
048                        boolean result2 = c2.foldCondition(a, b, null, false);
049                        if (result1) {
050                            assertTrue(result2);
051                        }
052                    }
053                }
054            }
055        }
056    }
057
058    @Test
059    public void testJoin() {
060        Random rand = new Random(13);
061        for (Condition c1 : Condition.values()) {
062            for (Condition c2 : Condition.values()) {
063                Condition join = c1.join(c2);
064                assertEquals(join, c2.join(c1));
065                if (join != null) {
066                    for (int i = 0; i < 1000; i++) {
067                        JavaConstant a = JavaConstant.forInt(rand.nextInt());
068                        JavaConstant b = JavaConstant.forInt(i < 100 ? a.asInt() : rand.nextInt());
069                        boolean result1 = c1.foldCondition(a, b, null, false);
070                        boolean result2 = c2.foldCondition(a, b, null, false);
071                        boolean resultJoin = join.foldCondition(a, b, null, false);
072                        if (result1 && result2) {
073                            assertTrue(resultJoin);
074                        } else {
075                            assertFalse(resultJoin);
076                        }
077                    }
078                }
079            }
080        }
081    }
082
083    @Test
084    public void testMeet() {
085        Random rand = new Random(13);
086        for (Condition c1 : Condition.values()) {
087            for (Condition c2 : Condition.values()) {
088                Condition meet = c1.meet(c2);
089                assertEquals(meet, c2.meet(c1));
090                if (meet != null) {
091                    for (int i = 0; i < 1000; i++) {
092                        JavaConstant a = JavaConstant.forInt(rand.nextInt());
093                        JavaConstant b = JavaConstant.forInt(i < 100 ? a.asInt() : rand.nextInt());
094                        boolean result1 = c1.foldCondition(a, b, null, false);
095                        boolean result2 = c2.foldCondition(a, b, null, false);
096                        boolean resultMeet = meet.foldCondition(a, b, null, false);
097                        if (result1 || result2) {
098                            assertTrue(resultMeet);
099                        } else {
100                            assertFalse(resultMeet);
101                        }
102                    }
103                }
104            }
105        }
106    }
107
108}