001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package db4ounit;
022:
023: public class ArrayAssert {
024:
025: public static void contains(long[] array, long expected) {
026: if (-1 != indexOf(array, expected)) {
027: return;
028: }
029: Assert.fail("Expecting '" + expected + "'.");
030: }
031:
032: public static void contains(Object[] array, Object[] expected) {
033: for (int i = 0; i < expected.length; i++) {
034: if (-1 == indexOf(array, expected[i])) {
035: Assert
036: .fail("Expecting contains '" + expected[i]
037: + "'.");
038: }
039: }
040: }
041:
042: public static int indexOf(Object[] array, Object expected) {
043: for (int i = 0; i < array.length; ++i) {
044: if (expected.equals(array[i])) {
045: return i;
046: }
047: }
048: return -1;
049: }
050:
051: public static void areEqual(Object[] expected, Object[] actual) {
052: if (expected == actual)
053: return;
054: if (expected == null || actual == null)
055: Assert.areSame(expected, actual);
056: Assert.areEqual(expected.length, actual.length);
057: Assert.areSame(expected.getClass(), actual.getClass());
058: for (int i = 0; i < expected.length; i++) {
059: Assert.areEqual(expected[i], actual[i], indexMessage(i));
060: }
061: }
062:
063: private static String indexMessage(int i) {
064: return "expected[" + i + "]";
065: }
066:
067: public static void areEqual(byte[] expected, byte[] actual) {
068: if (expected == actual)
069: return;
070: if (expected == null || actual == null)
071: Assert.areSame(expected, actual);
072: Assert.areEqual(expected.length, actual.length);
073: for (int i = 0; i < expected.length; i++) {
074: Assert.areEqual(expected[i], actual[i], indexMessage(i));
075: }
076: }
077:
078: public static void areNotEqual(byte[] expected, byte[] actual) {
079: Assert.areNotSame(expected, actual);
080: for (int i = 0; i < expected.length; i++) {
081: if (expected[i] != actual[i])
082: return;
083: }
084: Assert.isTrue(false);
085: }
086:
087: public static void areEqual(int[] expected, int[] actual) {
088: if (expected == actual)
089: return;
090: if (expected == null || actual == null)
091: Assert.areSame(expected, actual);
092: Assert.areEqual(expected.length, actual.length);
093: for (int i = 0; i < expected.length; i++) {
094: Assert.areEqual(expected[i], actual[i], indexMessage(i));
095: }
096: }
097:
098: public static void areEqual(double[] expected, double[] actual) {
099: if (expected == actual)
100: return;
101: if (expected == null || actual == null)
102: Assert.areSame(expected, actual);
103: Assert.areEqual(expected.length, actual.length);
104: for (int i = 0; i < expected.length; i++) {
105: Assert.areEqual(expected[i], actual[i], indexMessage(i));
106: }
107: }
108:
109: public static void areEqual(char[] expected, char[] actual) {
110: if (expected == actual)
111: return;
112: if (expected == null || actual == null)
113: Assert.areSame(expected, actual);
114: Assert.areEqual(expected.length, actual.length);
115: for (int i = 0; i < expected.length; i++) {
116: Assert.areEqual(expected[i], actual[i], indexMessage(i));
117: }
118: }
119:
120: private static int indexOf(long[] array, long expected) {
121: for (int i = 0; i < array.length; ++i) {
122: if (expected == array[i]) {
123: return i;
124: }
125: }
126: return -1;
127: }
128: }
|