001 /* 002 * Copyright (C) 2008-2010 by Holger Arndt 003 * 004 * This file is part of the Universal Java Matrix Package (UJMP). 005 * See the NOTICE file distributed with this work for additional 006 * information regarding copyright ownership and licensing. 007 * 008 * UJMP is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU Lesser General Public License as 010 * published by the Free Software Foundation; either version 2 011 * of the License, or (at your option) any later version. 012 * 013 * UJMP is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU Lesser General Public License for more details. 017 * 018 * You should have received a copy of the GNU Lesser General Public 019 * License along with UJMP; if not, write to the 020 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 021 * Boston, MA 02110-1301 USA 022 */ 023 024 package org.ujmp.core.util; 025 026 import java.util.Arrays; 027 028 import org.ujmp.core.Matrix; 029 030 public abstract class VerifyUtil { 031 032 public static final void assertTrue(boolean test, String message, Object... messageArgs) { 033 if (!test) { 034 String text = (messageArgs == null || messageArgs.length == 0) ? message : String 035 .format(message, messageArgs); 036 throw new IllegalArgumentException(text); 037 } 038 } 039 040 public static final void assertFalse(boolean test, String message, Object... messageArgs) { 041 if (!test) { 042 String text = (messageArgs == null || messageArgs.length == 0) ? message : String 043 .format(message, messageArgs); 044 throw new IllegalArgumentException(text); 045 } 046 } 047 048 public static final void assertTrue(boolean test, String message) { 049 if (!test) { 050 throw new IllegalArgumentException(message); 051 } 052 } 053 054 public static final void assertFalse(boolean test, String message) { 055 if (test) { 056 throw new IllegalArgumentException(message); 057 } 058 } 059 060 public static final void assertSameSize(final Matrix m1, final Matrix m2) { 061 assertTrue(Arrays.equals(m1.getSize(), m2.getSize()), "matrices have different sizes"); 062 } 063 064 public static void assertSameSize(Matrix... matrices) { 065 assertTrue(matrices.length > 1, "more than one matrix must be provided"); 066 for (int i = matrices.length; --i != 0;) { 067 assertTrue(Arrays.equals(matrices[i].getSize(), matrices[i - 1].getSize()), 068 "matrices have different sizes"); 069 } 070 } 071 072 public static void assertSameSize(double[][] source1, double[][] source2, double[][] target) { 073 assertNotNull(source1, "matrix1 cannot be null"); 074 assertNotNull(source2, "matrix2 cannot be null"); 075 assertNotNull(target, "matrix3 cannot be null"); 076 assertNotNull(source1[0], "matrix1 must be 2d"); 077 assertNotNull(source2[0], "matrix2 must be 2d"); 078 assertNotNull(target[0], "matrix3 must be 2d"); 079 assertEquals(source1.length, source2.length, "matrix1 and matrix2 have different sizes"); 080 assertEquals(source2.length, target.length, "matrix1 and matrix3 have different sizes"); 081 assertEquals(source1[0].length, source2[0].length, 082 "matrix1 and matrix2 have different sizes"); 083 assertEquals(source2[0].length, target[0].length, 084 "matrix1 and matrix3 have different sizes"); 085 } 086 087 public static void assertEquals(int i1, int i2, String message) { 088 assertTrue(i1 == i2, message); 089 } 090 091 public static void assertNotNull(Object o, String message) { 092 assertFalse(o == null, message); 093 } 094 095 public static void assertNull(Object o, String message) { 096 assertTrue(o == null, message); 097 } 098 099 public static void assertSameSize(double[] source1, double[] source2, double[] target) { 100 assertNotNull(source1, "matrix1 cannot be null"); 101 assertNotNull(source2, "matrix2 cannot be null"); 102 assertNotNull(target, "matrix3 cannot be null"); 103 assertEquals(source1.length, source2.length, "matrix1 and matrix2 have different sizes"); 104 assertEquals(source2.length, target.length, "matrix1 and matrix3 have different sizes"); 105 } 106 107 public static void assertSameSize(double[][] source, double[][] target) { 108 assertNotNull(source, "matrix1 cannot be null"); 109 assertNotNull(target, "matrix2 cannot be null"); 110 assertNotNull(source[0], "matrix1 must be 2d"); 111 assertNotNull(target[0], "matrix2 must be 2d"); 112 assertEquals(source.length, target.length, "matrix1 and matrix2 have different sizes"); 113 assertEquals(source[0].length, target[0].length, "matrix1 and matrix2 have different sizes"); 114 } 115 116 public static void assertSameSize(double[] source, double[] target) { 117 assertNotNull(source, "matrix1 cannot be null"); 118 assertNotNull(target, "matrix2 cannot be null"); 119 assertEquals(source.length, target.length, "matrix1 and matrix2 have different sizes"); 120 } 121 122 public static void assert2D(Matrix m) { 123 assertNotNull(m, "matrix cannot be null"); 124 assertEquals(m.getDimensionCount(), 2, "matrix is not 2d"); 125 } 126 127 public static void assertEquals(long l1, long l2, String message) { 128 assertTrue(l1 == l2, message); 129 } 130 }