001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.repository.testbench.sfs;
043:
044: import java.io.File;
045: import java.util.*;
046: import org.netbeans.modules.cnd.repository.util.LongHashMap;
047:
048: /**
049: * Tests LongHashMap
050: * @author Vladimir Kvashin
051: */
052: public class TestLongHashMap extends BaseTest {
053:
054: private int errCnt = 0;
055: private int cnt = 0;
056:
057: private Map lastReference;
058: private LongHashMap lastMap;
059: private Map lastChunkMap;
060:
061: private class Unit<K> {
062:
063: private Map<K, Long> reference;
064: private LongHashMap<K> map;
065:
066: public Unit(Map<K, Long> reference) {
067: this .reference = reference;
068: map = new LongHashMap<K>();
069: lastReference = reference;
070: lastMap = map;
071: // lastChunkMap = new HashMap<K, ChunkInfo>();
072: // for( Map.Entry<K, Long> e : reference.entrySet() ) {
073: // lastChunkMap.put(e.getKey(), ChunkInfoFactory.instance().createChunkInfo(e.getValue(), 0));
074: // }
075: }
076:
077: private boolean passed;
078:
079: public boolean test() {
080: passed = true;
081: for (Map.Entry<K, Long> e : reference.entrySet()) {
082:
083: K key = e.getKey();
084: long value = e.getValue().longValue();
085:
086: testEmpty(key);
087:
088: map.put(key, value);
089:
090: assertTrue(map.containsKey(key),
091: "Map should contain the key " + key
092: + " at this moment");
093: long v = map.get(key);
094: assertEquals(v, value, key, "");
095:
096: map.remove(key);
097: testEmpty(key);
098: map.put(key, value);
099:
100: cnt++;
101: }
102: return passed;
103: }
104:
105: private void testEmpty(K key) {
106: assertFalse(map.containsKey(key),
107: "Map shouldn't contain the key " + key
108: + " at this moment");
109: long v = map.get(key);
110: assertEquals(v, LongHashMap.NO_VALUE, key,
111: "Map doesn't contain the key");
112: }
113:
114: private void assertFalse(boolean condition, String text) {
115: assertTrue(!condition, text);
116: }
117:
118: private void assertTrue(boolean condition, String text) {
119: if (!condition) {
120: errCnt++;
121: System.err.printf("%s\n", text);
122: passed = false;
123: }
124: }
125:
126: private void assertEquals(long actualValue,
127: long referenceValue, K key, String preface) {
128: if (actualValue != referenceValue) {
129: errCnt++;
130: System.err.printf("%s key=%s value=%d; should be %d\n",
131: preface, key, actualValue, referenceValue);
132: passed = false;
133: }
134: }
135:
136: }
137:
138: public boolean test(List<String> args) {
139: boolean passed = test_a();
140: passed &= test_files(args);
141: report();
142: return passed;
143: }
144:
145: private void report() {
146: if (errCnt > 0) {
147: System.out.printf("FAILURE. Count: %d Errors: %d\n", cnt,
148: errCnt); // NOI18N
149: } else {
150: System.err.printf("SUCCESS. Count: %d \n", cnt);
151: }
152: }
153:
154: private boolean test_a() {
155: Map<String, Long> reference = new HashMap<String, Long>();
156: reference.put("One", 1L); // NOI18N
157: reference.put("Two", 2L); // NOI18N
158: return new Unit<String>(reference).test();
159: }
160:
161: private boolean test_files(List<String> args) {
162:
163: if (args.isEmpty()) {
164: return true;
165: }
166: System.out.printf("Creating test objects...\n"); // NOI18N
167:
168: Map<String, Long> reference_s = new HashMap<String, Long>();
169: Map<File, Long> reference_f = new HashMap<File, Long>();
170:
171: for (String path : args) {
172: collectFiles(path, reference_s, reference_f);
173: }
174:
175: return new Unit<String>(reference_s).test()
176: && new Unit<File>(reference_f).test();
177: }
178:
179: private void collectFiles(String path,
180: Map<String, Long> reference_s, Map<File, Long> reference_f) {
181: collectFiles(new File(path), reference_s, reference_f);
182: }
183:
184: private void collectFiles(File file, Map<String, Long> reference_s,
185: Map<File, Long> reference_f) {
186: if (file.exists()) {
187: reference_f.put(file, file.length());
188: reference_s.put(file.getAbsolutePath(), file.length());
189: if (file.isDirectory()) {
190: System.out.printf("Creating test objects from %s\n",
191: file.getPath()); // NOI18N
192: File[] files = file.listFiles();
193: if (files != null) {
194: for (int i = 0; i < files.length; i++) {
195: collectFiles(files[i], reference_s, reference_f);
196: }
197: }
198: }
199: }
200: }
201: }
|