alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (IntegerOperatorsTest.groovy)

This example Groovy source code file (IntegerOperatorsTest.groovy) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

character, character, groovytestcase, integer, integer, integeroperatorstest, long, should, should, unsupportedoperationexception, unsupportedoperationexception

The Groovy IntegerOperatorsTest.groovy source code

package groovy.operator

class IntegerOperatorsTest extends GroovyTestCase {

    def x
    def y
    def z
    
    void testPlus() {
        x = 2 + 2
        assert x == 4
        
        y = x + 1
        assert y == 5

        z = y + x + 1 + 2
        assert z == 12
    }
    
    void testCharacterPlus() {
        Character c1 = 1
        Character c2 = 2

        x = c2 + 2
        assert x == 4

        x = 2 + c2
        assert x == 4

        x = c2 + c2
        assert x == 4
          
        y = x + c1
        assert y == 5
          
        y = c1 + x
        assert y == 5

        z = y + x + c1 + 2
        assert z == 12

        z = y + x + 1 + c2
        assert z == 12

        z = y + x + c1 + c2
        assert z == 12
    }
    
    void testMinus() {
        x = 6 - 2
        assert x == 4
        
        y = x - 1
        assert y == 3
    }
    
    void testCharacterMinus() {
        Character c1 = 1
        Character c2 = 2
        Character c6 = 6

        x = c6 - 2
        assert x == 4

        x = 6 - c2
        assert x == 4

        x = c6 - c2
        assert x == 4
        
        y = x - c1
        assert y == 3
    }
    
    void testMultiply() {
        x = 3 * 2
        assert x == 6
        
        y = x * 2
        assert y == 12        
    }
    
    void testDivide() {
        x = 80 / 4
        assert x == 20.0 , "x = " + x
        
        y = x / 2
        assert y == 10.0 , "y = " + y
    }
    
    void testIntegerDivide() {
        x = 52.intdiv(3)
        assert x == 17 , "x = " + x
        
        y = x.intdiv(2)
        assert y == 8 , "y = " + y 
        
        y = 11
        y = y.intdiv(3)
        assert y == 3       
    }
    
    void testMod() {
        x = 100 % 3

        assert x == 1

        y = 11
        y %= 3
        assert y == 2
    }
    
    void testAnd() {
        x = 1 & 3

        assert x == 1

        x = 1.and(3)

        assert x == 1
    }
     
     void testOr() {
         x = 1 | 3

         assert x == 3

         x = 1 | 4

         assert x == 5

         x = 1.or(3)

         assert x == 3

         x = 1.or(4)

         assert x ==5
    }
    
    void testShiftOperators() {

        x = 8 >> 1
        assert x == 4
        assert x instanceof Integer

        x = 8 << 2
        assert x == 32
        assert x instanceof Integer

        x = 8L << 2
        assert x == 32
        assert x instanceof Long

        x = -16 >> 4
        assert x == -1

        x = -16 >>> 4
        assert x == 0xFFFFFFF

        //Ensure that the type of the right operand (shift distance) is ignored when calculating the
        //result.  This is how java works, and for these operators, it makes sense to keep that behavior.
        x = Integer.MAX_VALUE << 1L
        assert x == -2
        assert x instanceof Integer

        x = new Long(Integer.MAX_VALUE).longValue() << 1
        assert x == 0xfffffffe
        assert x instanceof Long

        //The left operand (shift value) must be an integral type
        try {
            x = 8.0F >> 2
            fail("Should catch UnsupportedOperationException");
        } catch (UnsupportedOperationException uoe) {
        }

        //The right operand (shift distance) must be an integral type
        try {
            x = 8 >> 2.0
            fail("Should catch UnsupportedOperationException");
        } catch (UnsupportedOperationException uoe) {
        }
    }
}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy IntegerOperatorsTest.groovy source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.