home | career | drupal | java | mac | mysql | perl | scala | uml | unix

Groovy example source code file (MultiDimArraysTest.groovy)

This example Groovy source code file (MultiDimArraysTest.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

groovytestcase, groovytestcase, i, i, multidimarraystest, multidimarraystest, object, object

The Groovy MultiDimArraysTest.groovy source code

/*
 * Copyright 2003-2010 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package groovy

/**
 * Expose how to deal with multi-dimensional Arrays until this is supported at the language level.
 * @author Dierk Koenig
 * @author Jochen Theodorou
 */
class MultiDimArraysTest extends GroovyTestCase {

    // todo: enable as soon as multi dims are supported
    void testCallTwoDimStringArray() {
        def someArrayOfStringArrays = new SomeClass().anArrayOfStringArrays()
        assert 1 == someArrayOfStringArrays.size()
    }

    void testCallTwoDimStringArrayWorkaround() {
        def someArrayOfStringArrays = new SomeClass().anArrayOfStringArraysWorkaround()
        assert 1 == someArrayOfStringArrays.size()
        assert "whatever" == someArrayOfStringArrays[0][0]
        for (i in 0..<someArrayOfStringArrays.size()) {
            assert someArrayOfStringArrays[i]
        }
    }

    void testCallTwoDimStringArrayWorkaroundWithNull() {
        def someArrayOfStringArrays = new SomeClass().anArrayOfStringArraysWorkaround()
        assert 1 == someArrayOfStringArrays.size()
        assert "whatever" == someArrayOfStringArrays[0][0]
        someArrayOfStringArrays.each() { assert it}
    }

    void testInsideGroovyMultiDimReplacement() {
        Object[] someArrayOfStringArrays = [["a", "a", "a"], ["b", "b", "b", null]]
        assert "a" == someArrayOfStringArrays[0][0]
        someArrayOfStringArrays.each() { assert it}
    }

    void testMultiDimCreationWithSizes() {
        Object[][] objectArray = new Object[2][5]
        assert objectArray.length == 2
        objectArray.each {
            assert it.length == 5
            it.each { assert it == null }
        }
    }

    void testMultiDimCreationWithoutSizeAtEnd() {
        def array = new int[5][6][]
        assert array.class.name == "[[[I"
        assert array[0].class.name == "[[I"
        assert array[0][0] == null
    }

    void testMultiDimArrayForCustomClass() {
        def ff = new MultiDimArraysTest[3][4]
        assert "[[Lgroovy.MultiDimArraysTest;" == ff.class.name;
    }

}

Other Groovy examples (source code examples)

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

new blog posts

 

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