rt/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 708 vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java@59d5596a9c6c
child 789 bb7506513353
permissions -rw-r--r--
Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
jaroslav@106
     1
/**
jaroslav@106
     2
 * Back 2 Browser Bytecode Translator
jaroslav@106
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@106
     4
 *
jaroslav@106
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@106
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@106
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@106
     8
 *
jaroslav@106
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@106
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@106
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@106
    12
 * GNU General Public License for more details.
jaroslav@106
    13
 *
jaroslav@106
    14
 * You should have received a copy of the GNU General Public License
jaroslav@106
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@106
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@106
    17
 */
jaroslav@22
    18
package org.apidesign.vm4brwsr;
jaroslav@0
    19
jaroslav@0
    20
import static org.testng.Assert.*;
jaroslav@103
    21
import org.testng.annotations.BeforeClass;
jaroslav@0
    22
import org.testng.annotations.Test;
jaroslav@0
    23
jaroslav@0
    24
/** Checks the basic behavior of the translator.
jaroslav@0
    25
 *
jaroslav@0
    26
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@0
    27
 */
jaroslav@0
    28
public class StaticMethodTest {
jaroslav@0
    29
    @Test public void threePlusFour() throws Exception {
jaroslav@2
    30
        assertExec(
jaroslav@2
    31
            "Should be seven", 
jaroslav@248
    32
            StaticMethod.class, "sum__III", 
jaroslav@2
    33
            Double.valueOf(7), 
jaroslav@2
    34
            3, 4
jaroslav@2
    35
        );
jaroslav@0
    36
    }
jaroslav@0
    37
jaroslav@173
    38
    @Test public void checkReallyInitializedValues() throws Exception {
jaroslav@173
    39
        assertExec(
jaroslav@173
    40
            "Return true", 
jaroslav@248
    41
            StaticMethod.class, "isNull__Z", 
jaroslav@173
    42
            Double.valueOf(1)
jaroslav@173
    43
        );
jaroslav@173
    44
    }
jaroslav@173
    45
jaroslav@1
    46
    @Test public void powerOfThree() throws Exception {
jaroslav@2
    47
        assertExec(
jaroslav@2
    48
            "Should be nine", 
jaroslav@248
    49
            StaticMethod.class, "power__FF", 
jaroslav@2
    50
            Double.valueOf(9),
jaroslav@2
    51
            3.0f
jaroslav@2
    52
        );
jaroslav@1
    53
    }
jaroslav@1
    54
jaroslav@48
    55
    @Test public void minusOne() throws Exception {
jaroslav@48
    56
        assertExec(
jaroslav@48
    57
            "Should be minus one", 
jaroslav@248
    58
            StaticMethod.class, "minusOne__I", 
jaroslav@48
    59
            Double.valueOf(-1)
jaroslav@48
    60
        );
jaroslav@48
    61
    }
jaroslav@48
    62
jaroslav@2
    63
    @Test public void doubleWithoutLong() throws Exception {
jaroslav@2
    64
        assertExec(
jaroslav@2
    65
            "Should be two",
jaroslav@248
    66
            StaticMethod.class, "minus__DDJ", 
jaroslav@2
    67
            Double.valueOf(2),
jaroslav@2
    68
            3.0d, 1l
jaroslav@2
    69
        );
jaroslav@2
    70
    }
jaroslav@600
    71
    
jaroslav@600
    72
    @Test public void rintNegativeUp() throws Exception {
jaroslav@600
    73
        final double cnts = -453904.634;
jaroslav@600
    74
        assertExec(
jaroslav@600
    75
            "Should round up to end with 5",
jaroslav@600
    76
            Math.class, "rint__DD", 
jaroslav@600
    77
            -453905.0, cnts
jaroslav@600
    78
        );
jaroslav@600
    79
    }
jaroslav@600
    80
jaroslav@600
    81
    @Test public void rintNegativeDown() throws Exception {
jaroslav@600
    82
        final double cnts = -453904.434;
jaroslav@600
    83
        assertExec(
jaroslav@600
    84
            "Should round up to end with 4",
jaroslav@600
    85
            Math.class, "rint__DD", 
jaroslav@600
    86
            -453904.0, cnts
jaroslav@600
    87
        );
jaroslav@600
    88
    }
jaroslav@600
    89
jaroslav@600
    90
    @Test public void rintPositiveUp() throws Exception {
jaroslav@600
    91
        final double cnts = 453904.634;
jaroslav@600
    92
        assertExec(
jaroslav@600
    93
            "Should round up to end with 5",
jaroslav@600
    94
            Math.class, "rint__DD", 
jaroslav@600
    95
            453905.0, cnts
jaroslav@600
    96
        );
jaroslav@600
    97
    }
jaroslav@600
    98
    @Test public void rintPositiveDown() throws Exception {
jaroslav@600
    99
        final double cnts = 453904.434;
jaroslav@600
   100
        assertExec(
jaroslav@600
   101
            "Should round up to end with 4",
jaroslav@600
   102
            Math.class, "rint__DD", 
jaroslav@600
   103
            453904.0, cnts
jaroslav@600
   104
        );
jaroslav@600
   105
    }
jaroslav@600
   106
    @Test public void rintOneHalf() throws Exception {
jaroslav@600
   107
        final double cnts = 1.5;
jaroslav@600
   108
        assertExec(
jaroslav@600
   109
            "Should round up to end with 2",
jaroslav@600
   110
            Math.class, "rint__DD", 
jaroslav@600
   111
            2.0, cnts
jaroslav@600
   112
        );
jaroslav@600
   113
    }
jaroslav@600
   114
    @Test public void rintNegativeOneHalf() throws Exception {
jaroslav@600
   115
        final double cnts = -1.5;
jaroslav@600
   116
        assertExec(
jaroslav@600
   117
            "Should round up to end with 2",
jaroslav@600
   118
            Math.class, "rint__DD", 
jaroslav@600
   119
            -2.0, cnts
jaroslav@600
   120
        );
jaroslav@600
   121
    }
jaroslav@600
   122
    @Test public void rintTwoAndHalf() throws Exception {
jaroslav@600
   123
        final double cnts = 2.5;
jaroslav@600
   124
        assertExec(
jaroslav@600
   125
            "Should round up to end with 2",
jaroslav@600
   126
            Math.class, "rint__DD", 
jaroslav@600
   127
            2.0, cnts
jaroslav@600
   128
        );
jaroslav@600
   129
    }
jaroslav@600
   130
    @Test public void rintNegativeTwoOneHalf() throws Exception {
jaroslav@600
   131
        final double cnts = -2.5;
jaroslav@600
   132
        assertExec(
jaroslav@600
   133
            "Should round up to end with 2",
jaroslav@600
   134
            Math.class, "rint__DD", 
jaroslav@600
   135
            -2.0, cnts
jaroslav@600
   136
        );
jaroslav@600
   137
    }
jaroslav@3
   138
jaroslav@605
   139
    @Test public void ieeeReminder1() throws Exception {
jaroslav@605
   140
        assertExec(
jaroslav@605
   141
            "Same result 1",
jaroslav@605
   142
            Math.class, "IEEEremainder__DDD", 
jaroslav@605
   143
            Math.IEEEremainder(10.0, 4.5), 10.0, 4.5
jaroslav@605
   144
        );
jaroslav@605
   145
    }
jaroslav@605
   146
jaroslav@605
   147
    @Test public void ieeeReminder2() throws Exception {
jaroslav@605
   148
        assertExec(
jaroslav@605
   149
            "Same result 1",
jaroslav@605
   150
            Math.class, "IEEEremainder__DDD", 
jaroslav@605
   151
            Math.IEEEremainder(Integer.MAX_VALUE, -4.5), Integer.MAX_VALUE, -4.5
jaroslav@605
   152
        );
jaroslav@605
   153
    }
jaroslav@605
   154
jaroslav@3
   155
    @Test public void divAndRound() throws Exception {
jaroslav@3
   156
        assertExec(
jaroslav@3
   157
            "Should be rounded to one",
jaroslav@248
   158
            StaticMethod.class, "div__IBD", 
jaroslav@3
   159
            Double.valueOf(1),
jaroslav@3
   160
            3, 3.75
jaroslav@3
   161
        );
jaroslav@3
   162
    }
jaroslav@3
   163
    @Test public void mixedMethodFourParams() throws Exception {
jaroslav@3
   164
        assertExec(
jaroslav@3
   165
            "Should be two",
jaroslav@248
   166
            StaticMethod.class, "mix__IIJBD", 
jaroslav@3
   167
            Double.valueOf(20),
jaroslav@3
   168
            2, 10l, 5, 2.0
jaroslav@3
   169
        );
jaroslav@3
   170
    }
jaroslav@4
   171
    @Test public void factRec() throws Exception {
jaroslav@4
   172
        assertExec(
jaroslav@4
   173
            "Factorial of 5 is 120",
jaroslav@248
   174
            StaticMethod.class, "factRec__JI", 
jaroslav@4
   175
            Double.valueOf(120),
jaroslav@4
   176
            5
jaroslav@4
   177
        );
jaroslav@4
   178
    }
jaroslav@5
   179
    @Test public void factIter() throws Exception {
jaroslav@5
   180
        assertExec(
jaroslav@5
   181
            "Factorial of 5 is 120",
jaroslav@248
   182
            StaticMethod.class, "factIter__JI", 
jaroslav@5
   183
            Double.valueOf(120),
jaroslav@5
   184
            5
jaroslav@5
   185
        );
jaroslav@5
   186
    }
jaroslav@2
   187
    
jaroslav@6
   188
    @Test public void xor() throws Exception {
jaroslav@6
   189
        assertExec(
jaroslav@6
   190
            "Xor is 4",
jaroslav@248
   191
            StaticMethod.class, "xor__JIJ",
jaroslav@6
   192
            Double.valueOf(4),
jaroslav@6
   193
            7,
jaroslav@6
   194
            3
jaroslav@6
   195
        );
jaroslav@6
   196
    }
jaroslav@6
   197
    
jaroslav@7
   198
    @Test public void or() throws Exception {
jaroslav@7
   199
        assertExec(
jaroslav@7
   200
            "Or will be 7",
jaroslav@248
   201
            StaticMethod.class, "orOrAnd__JZII",
jaroslav@7
   202
            Double.valueOf(7),
jaroslav@7
   203
            true,
jaroslav@7
   204
            4,
jaroslav@7
   205
            3
jaroslav@7
   206
        );
jaroslav@7
   207
    }
jaroslav@46
   208
    @Test public void nullCheck() throws Exception {
jaroslav@46
   209
        assertExec(
jaroslav@46
   210
            "Returns nothing",
jaroslav@248
   211
            StaticMethod.class, "none__Ljava_lang_Object_2II",
jaroslav@46
   212
            null, 1, 3
jaroslav@46
   213
        );
jaroslav@46
   214
    }
jaroslav@7
   215
    @Test public void and() throws Exception {
jaroslav@7
   216
        assertExec(
jaroslav@7
   217
            "And will be 3",
jaroslav@248
   218
            StaticMethod.class, "orOrAnd__JZII",
jaroslav@7
   219
            Double.valueOf(3),
jaroslav@7
   220
            false,
jaroslav@7
   221
            7,
jaroslav@7
   222
            3
jaroslav@7
   223
        );
jaroslav@7
   224
    }
jaroslav@9
   225
    @Test public void inc4() throws Exception {
jaroslav@9
   226
        assertExec(
jaroslav@9
   227
            "It will be 4",
jaroslav@248
   228
            StaticMethod.class, "inc4__I",
jaroslav@9
   229
            Double.valueOf(4)
jaroslav@9
   230
        );
jaroslav@9
   231
    }
jaroslav@7
   232
    
jaroslav@93
   233
    @Test public void shiftLeftInJava() throws Exception {
jaroslav@93
   234
        int res = StaticMethod.shiftLeft(1, 8);
jaroslav@93
   235
        assertEquals(res, 256);
jaroslav@93
   236
    }
jaroslav@93
   237
jaroslav@93
   238
    @Test public void shiftLeftInJS() throws Exception {
jaroslav@93
   239
        assertExec(
jaroslav@93
   240
            "Setting 9th bit",
jaroslav@248
   241
            StaticMethod.class, "shiftLeft__III",
jaroslav@93
   242
            Double.valueOf(256),
jaroslav@93
   243
            1, 8
jaroslav@93
   244
        );
jaroslav@93
   245
    }
jaroslav@93
   246
jaroslav@93
   247
    @Test public void shiftRightInJava() throws Exception {
jaroslav@93
   248
        int res = StaticMethod.shiftArithmRight(-8, 3, true);
jaroslav@93
   249
        assertEquals(res, -1);
jaroslav@93
   250
    }
jaroslav@93
   251
jaroslav@93
   252
    @Test public void shiftRightInJS() throws Exception {
jaroslav@93
   253
        assertExec(
jaroslav@93
   254
            "Get -1",
jaroslav@248
   255
            StaticMethod.class, "shiftArithmRight__IIIZ",
jaroslav@93
   256
            Double.valueOf(-1),
jaroslav@93
   257
            -8, 3, true
jaroslav@93
   258
        );
jaroslav@93
   259
    }
jaroslav@93
   260
    @Test public void unsignedShiftRightInJava() throws Exception {
jaroslav@93
   261
        int res = StaticMethod.shiftArithmRight(8, 3, false);
jaroslav@93
   262
        assertEquals(res, 1);
jaroslav@93
   263
    }
jaroslav@93
   264
jaroslav@93
   265
    @Test public void unsignedShiftRightInJS() throws Exception {
jaroslav@93
   266
        assertExec(
jaroslav@93
   267
            "Get -1",
jaroslav@248
   268
            StaticMethod.class, "shiftArithmRight__IIIZ",
jaroslav@93
   269
            Double.valueOf(1),
jaroslav@93
   270
            8, 3, false
jaroslav@93
   271
        );
jaroslav@93
   272
    }
jaroslav@93
   273
    
jaroslav@94
   274
    @Test public void javaScriptBody() throws Exception {
jaroslav@94
   275
        assertExec(
jaroslav@94
   276
            "JavaScript string",
jaroslav@248
   277
            StaticMethod.class, "i2s__Ljava_lang_String_2II",
jaroslav@94
   278
            "333",
jaroslav@99
   279
            330, 3
jaroslav@94
   280
        );
jaroslav@94
   281
    }
jaroslav@94
   282
    
jaroslav@115
   283
    @Test public void switchJarda() throws Exception {
jaroslav@115
   284
        assertExec(
jaroslav@115
   285
            "The expected value",
jaroslav@248
   286
            StaticMethod.class, "swtch__Ljava_lang_String_2I",
jaroslav@115
   287
            "Jarda",
jaroslav@115
   288
            0
jaroslav@115
   289
        );
jaroslav@115
   290
    }
jaroslav@115
   291
    
jaroslav@115
   292
    @Test public void switchDarda() throws Exception {
jaroslav@115
   293
        assertExec(
jaroslav@115
   294
            "The expected value",
jaroslav@248
   295
            StaticMethod.class, "swtch__Ljava_lang_String_2I",
jaroslav@115
   296
            "Darda",
jaroslav@115
   297
            1
jaroslav@115
   298
        );
jaroslav@115
   299
    }
jaroslav@115
   300
    @Test public void switchParda() throws Exception {
jaroslav@115
   301
        assertExec(
jaroslav@115
   302
            "The expected value",
jaroslav@248
   303
            StaticMethod.class, "swtch2__Ljava_lang_String_2I",
jaroslav@115
   304
            "Parda",
jaroslav@115
   305
            22
jaroslav@115
   306
        );
jaroslav@115
   307
    }
jaroslav@115
   308
    @Test public void switchMarda() throws Exception {
jaroslav@115
   309
        assertExec(
jaroslav@115
   310
            "The expected value",
jaroslav@248
   311
            StaticMethod.class, "swtch__Ljava_lang_String_2I",
jaroslav@115
   312
            "Marda",
jaroslav@115
   313
            -433
jaroslav@115
   314
        );
jaroslav@115
   315
    }
jaroslav@115
   316
    
jaroslav@291
   317
    @Test public void checkNullCast() throws Exception {
jaroslav@291
   318
        assertExec("Null can be cast to any type",
jaroslav@291
   319
            StaticMethod.class, "castNull__Ljava_lang_String_2Z", 
jaroslav@291
   320
            null, true
jaroslav@291
   321
        );
jaroslav@291
   322
    }
jaroslav@291
   323
    
jaroslav@708
   324
    private static TestVM code;
jaroslav@103
   325
    
jaroslav@103
   326
    @BeforeClass 
jaroslav@103
   327
    public void compileTheCode() throws Exception {
jaroslav@103
   328
        StringBuilder sb = new StringBuilder();
jaroslav@708
   329
        code = TestVM.compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
jaroslav@2
   330
    }
jaroslav@2
   331
jaroslav@708
   332
    private void assertExec(
jaroslav@708
   333
        String msg, Class<?> clazz, String method, 
jaroslav@708
   334
        Object ret, Object... args
jaroslav@708
   335
    ) throws Exception {
jaroslav@708
   336
        code.assertExec(msg, clazz, method, ret, args);
jaroslav@298
   337
    }
jaroslav@0
   338
}