|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * SCIFIO library for reading and converting scientific file formats. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2011 - 2014 Board of Regents of the University of |
| 6 | + * Wisconsin-Madison |
| 7 | + * %% |
| 8 | + * Redistribution and use in source and binary forms, with or without |
| 9 | + * modification, are permitted provided that the following conditions are met: |
| 10 | + * |
| 11 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer. |
| 13 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 14 | + * this list of conditions and the following disclaimer in the documentation |
| 15 | + * and/or other materials provided with the distribution. |
| 16 | + * |
| 17 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 21 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 | + * POSSIBILITY OF SUCH DAMAGE. |
| 28 | + * #L% |
| 29 | + */ |
| 30 | + |
| 31 | +package org.scijava.util; |
| 32 | + |
| 33 | +import static org.junit.Assert.assertEquals; |
| 34 | +import static org.junit.Assert.fail; |
| 35 | + |
| 36 | +import org.junit.Test; |
| 37 | + |
| 38 | +/** Unit tests for {@link ArrayUtils}. */ |
| 39 | +public class ArrayUtilsTest { |
| 40 | + |
| 41 | + @Test |
| 42 | + public void testSafeMultiply32() { |
| 43 | + // test vacuous edge cases |
| 44 | + boolean ok = false; |
| 45 | + try { |
| 46 | + ArrayUtils.safeMultiply32(null); |
| 47 | + } |
| 48 | + catch (final NullPointerException e) { |
| 49 | + ok = true; |
| 50 | + } |
| 51 | + if (!ok) fail("Expected NullPointerException"); |
| 52 | + assertSafeMultiply32Pass(0); |
| 53 | + |
| 54 | + // test simple cases |
| 55 | + assertSafeMultiply32Pass(1, 1); |
| 56 | + assertSafeMultiply32Pass(54, 9, 6); |
| 57 | + assertSafeMultiply32Pass(1037836800, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13); |
| 58 | + |
| 59 | + // test invalid arguments |
| 60 | + assertSafeMultiply32Fail("Invalid array size: -1", -1); |
| 61 | + assertSafeMultiply32Fail("Invalid array size: 0", 0); |
| 62 | + |
| 63 | + // test edge cases near Integer.MAX_VALUE |
| 64 | + assertSafeMultiply32Pass(2147483646, Integer.MAX_VALUE / 2, 2); |
| 65 | + assertSafeMultiply32Fail("Array size too large: 1073741824 x 2", |
| 66 | + Integer.MAX_VALUE / 2 + 1, 2); |
| 67 | + assertSafeMultiply32Pass(2147441940, 46340, 46341); |
| 68 | + assertSafeMultiply32Fail("Array size too large: 46341 x 46341", 46341, |
| 69 | + 46341); |
| 70 | + assertSafeMultiply32Fail("Array size too large: 46340 x 46342", 46340, |
| 71 | + 46342); |
| 72 | + assertSafeMultiply32Pass(2147418112, 65536, 32767); |
| 73 | + assertSafeMultiply32Pass(2147450880, 65535, 32768); |
| 74 | + assertSafeMultiply32Fail("Array size too large: 65536 x 32768", 65536, |
| 75 | + 32768); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testSafeMultiply64() { |
| 80 | + // test vacuous edge cases |
| 81 | + boolean ok = false; |
| 82 | + try { |
| 83 | + ArrayUtils.safeMultiply64(null); |
| 84 | + } |
| 85 | + catch (final NullPointerException e) { |
| 86 | + ok = true; |
| 87 | + } |
| 88 | + if (!ok) fail("Expected NullPointerException"); |
| 89 | + assertSafeMultiply64Pass(0); |
| 90 | + |
| 91 | + // test simple cases |
| 92 | + assertSafeMultiply64Pass(1, 1); |
| 93 | + assertSafeMultiply64Pass(54, 9, 6); |
| 94 | + assertSafeMultiply64Pass(3632428800L, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14); |
| 95 | + |
| 96 | + // test invalid arguments |
| 97 | + assertSafeMultiply64Fail("Invalid array size: -1", -1); |
| 98 | + assertSafeMultiply64Fail("Invalid array size: 0", 0); |
| 99 | + |
| 100 | + // test edge cases near Long.MAX_VALUE |
| 101 | + assertSafeMultiply64Pass(9223372036854775806L, Long.MAX_VALUE / 6, 2, 3); |
| 102 | + assertSafeMultiply64Fail( |
| 103 | + "Array size too large: 1537228672809129302 x 2 x 3", |
| 104 | + Long.MAX_VALUE / 6 + 1, 2, 3); |
| 105 | + assertSafeMultiply64Pass(9223372033963249500L, 3037000499L, 3037000500L); |
| 106 | + assertSafeMultiply64Fail("Array size too large: 3037000500 x 3037000500", |
| 107 | + 3037000500L, 3037000500L); |
| 108 | + assertSafeMultiply64Fail("Array size too large: 3037000499 x 3037000501", |
| 109 | + 3037000499L, 3037000501L); |
| 110 | + } |
| 111 | + |
| 112 | + // -- Helper methods -- |
| 113 | + |
| 114 | + private void |
| 115 | + assertSafeMultiply32Pass(final int expected, final long... sizes) |
| 116 | + { |
| 117 | + assertEquals(expected, ArrayUtils.safeMultiply32(sizes)); |
| 118 | + } |
| 119 | + |
| 120 | + private void assertSafeMultiply32Fail(final String msg, final long... sizes) { |
| 121 | + int actual; |
| 122 | + try { |
| 123 | + actual = ArrayUtils.safeMultiply32(sizes); |
| 124 | + } |
| 125 | + catch (final IllegalArgumentException e) { |
| 126 | + assertEquals(e.getMessage(), msg); |
| 127 | + return; |
| 128 | + } |
| 129 | + fail("Safe multiply succeeded with value: " + actual); |
| 130 | + } |
| 131 | + |
| 132 | + private void assertSafeMultiply64Pass(final long expected, |
| 133 | + final long... sizes) |
| 134 | + { |
| 135 | + assertEquals(expected, ArrayUtils.safeMultiply64(sizes)); |
| 136 | + } |
| 137 | + |
| 138 | + private void assertSafeMultiply64Fail(final String msg, final long... sizes) { |
| 139 | + long actual; |
| 140 | + try { |
| 141 | + actual = ArrayUtils.safeMultiply64(sizes); |
| 142 | + } |
| 143 | + catch (final IllegalArgumentException e) { |
| 144 | + assertEquals(e.getMessage(), msg); |
| 145 | + return; |
| 146 | + } |
| 147 | + fail("Safe multiply succeeded with value: " + actual); |
| 148 | + } |
| 149 | + |
| 150 | +} |
0 commit comments