001/*
002 * Copyright (c) 2013, 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.truffle.test;
024
025import static org.hamcrest.CoreMatchers.*;
026import static org.junit.Assert.*;
027
028import org.junit.*;
029import org.junit.experimental.theories.*;
030import org.junit.runner.*;
031
032import com.oracle.graal.truffle.*;
033import com.oracle.truffle.api.*;
034
035@RunWith(Theories.class)
036public class TruffleStampTest {
037
038    private static final Object TYPE1 = new Object();
039    private static final Object TYPE2 = new Object();
040
041    // @formatter:off
042    @DataPoints public static Object[] data = new Object[]{1, 2, 1.0d, 2.0d, "1", "2", null,
043                    new TestTypedObject(TYPE1), new TestTypedObject(TYPE1), new TestTypedObject(TYPE2), //
044                    new Object[]{1, "a", new TestTypedObject(TYPE1)}, new Object[0]};
045    // @formatter:on
046
047    private TruffleStamp stamp;
048
049    @Before
050    public void setUp() {
051        this.stamp = DefaultTruffleStamp.getInstance();
052    }
053
054    @Theory
055    public void testOne1(Object value) {
056        assertThat(stamp.joinValue(value).isCompatible(value), is(true));
057    }
058
059    @Theory
060    public void testOne2(Object value) {
061        assertThat(stamp.join(stamp.joinValue(value)).isCompatible(value), is(true));
062    }
063
064    @Theory
065    public void testOne3(Object value) {
066        assertThat(stamp.joinValue(value).equals(stamp.joinValue(value)), is(true));
067    }
068
069    @Theory
070    public void testOne4(Object value) {
071        assertThat(stamp.isCompatible(value), is(false));
072    }
073
074    @Theory
075    public void testOne5(Object value) {
076        TruffleStamp stamp1 = stamp.joinValue(value);
077        assertThat(stamp1.joinValue(value), sameInstance(stamp1));
078    }
079
080    @Theory
081    public void testOne6(Object value) {
082        TruffleStamp stamp1 = stamp.joinValue(value);
083        TruffleStamp stamp2 = stamp.joinValue(value);
084        assertThat(stamp1.join(stamp2), sameInstance(stamp1));
085    }
086
087    @Theory
088    public void testOne7(Object value1, Object value2) {
089        assertThat(stamp.joinValue(value1).joinValue(value2).toStringShort(), is(notNullValue()));
090        assertThat(stamp.joinValue(value1).joinValue(value2).toString(), is(notNullValue()));
091        assertThat(stamp.joinValue(value1).joinValue(value2).hashCode(), is(stamp.joinValue(value1).joinValue(value2).hashCode()));
092    }
093
094    @Theory
095    public void testTwo1(Object value1, Object value2) {
096        TruffleStamp stamp1 = stamp.joinValue(value1).joinValue(value2);
097        assertThat(stamp1.isCompatible(value1), is(true));
098        assertThat(stamp1.isCompatible(value2), is(true));
099    }
100
101    @Theory
102    public void testTwo2(Object value1, Object value2) {
103        TruffleStamp stamp1 = stamp.join(stamp.joinValue(value1)).join(stamp.joinValue(value2));
104        assertThat(stamp1.isCompatible(value1), is(true));
105        assertThat(stamp1.isCompatible(value2), is(true));
106    }
107
108    @Theory
109    public void testTwo3(Object value1, Object value2) {
110        TruffleStamp stamp1 = stamp.joinValue(value1).joinValue(value2);
111        TruffleStamp stamp2 = stamp.joinValue(value1).joinValue(value2);
112        assertThat(stamp1.equals(stamp2), is(true));
113    }
114
115    @Theory
116    public void testThree1(Object value1, Object value2, Object value3) {
117        TruffleStamp stamp1 = stamp.joinValue(value1).joinValue(value2).joinValue(value3);
118        assertThat(stamp1.isCompatible(value1), is(true));
119        assertThat(stamp1.isCompatible(value2), is(true));
120        assertThat(stamp1.isCompatible(value3), is(true));
121    }
122
123    @Theory
124    public void testThree2(Object value1, Object value2, Object value3) {
125        TruffleStamp stamp1 = stamp.join(stamp.joinValue(value1)).join(stamp.joinValue(value2)).join(stamp.joinValue(value3));
126        assertThat(stamp1.isCompatible(value1), is(true));
127        assertThat(stamp1.isCompatible(value2), is(true));
128    }
129
130    @Theory
131    public void testThree3(Object value1, Object value2, Object value3) {
132        TruffleStamp stamp1 = stamp.joinValue(value1).joinValue(value2).joinValue(value3);
133        TruffleStamp stamp2 = stamp.joinValue(value1).joinValue(value2).joinValue(value3);
134        assertThat(stamp1.equals(stamp2), is(true));
135    }
136
137    @Theory
138    public void testThree4(Object value1, Object value2, Object value3) {
139        TruffleStamp stamp1 = stamp.joinValue(value1).join(stamp.joinValue(value2).joinValue(value3));
140        assertThat(stamp1.isCompatible(value1), is(true));
141        assertThat(stamp1.isCompatible(value2), is(true));
142    }
143
144    @Theory
145    public void testArray1(Object value1, Object value2, Object value3) {
146        Object[] values = new Object[]{value1, value2, value3};
147        stamp = stamp.joinValue(values);
148        assertThat(stamp.isCompatible(values), is(true));
149    }
150
151    @Theory
152    public void testArray2(Object value1, Object value2, Object value3) {
153        Object[] values = new Object[]{value1, value2, value3};
154        assertThat(stamp.joinValue(values).equals(stamp.joinValue(values)), is(true));
155    }
156
157    private static final class TestTypedObject implements TypedObject {
158
159        private final Object type;
160
161        public TestTypedObject(Object type) {
162            this.type = type;
163        }
164
165        public Object getTypeIdentifier() {
166            return type;
167        }
168
169    }
170
171}