1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.harmony.tests.java.util.zip; 18 19 import java.util.zip.CRC32; 20 21 public class CRC32Test extends junit.framework.TestCase { 22 23 /** 24 * java.util.zip.CRC32#CRC32() 25 */ 26 public void test_Constructor() { 27 // test methods of java.util.zip.CRC32() 28 CRC32 crc = new CRC32(); 29 assertEquals("Constructor of CRC32 failed", 0, crc.getValue()); 30 } 31 32 /** 33 * java.util.zip.CRC32#getValue() 34 */ 35 public void test_getValue() { 36 // test methods of java.util.zip.crc32.getValue() 37 CRC32 crc = new CRC32(); 38 assertEquals("getValue() should return a zero as a result of constructing a CRC32 instance", 39 0, crc.getValue()); 40 41 crc.reset(); 42 crc.update(Integer.MAX_VALUE); 43 // System.out.print("value of crc " + crc.getValue()); 44 // Ran JDK and discovered that the value of the CRC should be 45 // 4278190080 46 assertEquals("update(max) failed to update the checksum to the correct value ", 47 4278190080L, crc.getValue()); 48 49 crc.reset(); 50 byte byteEmpty[] = new byte[10000]; 51 crc.update(byteEmpty); 52 // System.out.print("value of crc"+crc.getValue()); 53 // Ran JDK and discovered that the value of the CRC should be 54 // 1295764014 55 assertEquals("update(byte[]) failed to update the checksum to the correct value ", 56 1295764014L, crc.getValue()); 57 58 crc.reset(); 59 crc.update(1); 60 // System.out.print("value of crc"+crc.getValue()); 61 // Ran JDK and discovered that the value of the CRC should be 62 // 2768625435 63 // assertEquals("update(int) failed to update the checksum to the correct 64 // value ",2768625435L, crc.getValue()); 65 crc.reset(); 66 assertEquals("reset failed to reset the checksum value to zero", 0, crc 67 .getValue()); 68 } 69 70 /** 71 * java.util.zip.CRC32#reset() 72 */ 73 public void test_reset() { 74 // test methods of java.util.zip.crc32.reset() 75 CRC32 crc = new CRC32(); 76 crc.update(1); 77 // System.out.print("value of crc"+crc.getValue()); 78 // Ran JDK and discovered that the value of the CRC should be 79 // 2768625435 80 assertEquals("update(int) failed to update the checksum to the correct value ", 81 2768625435L, crc.getValue()); 82 crc.reset(); 83 assertEquals("reset failed to reset the checksum value to zero", 0, crc 84 .getValue()); 85 86 } 87 88 /** 89 * java.util.zip.CRC32#update(int) 90 */ 91 public void test_updateI() { 92 // test methods of java.util.zip.crc32.update(int) 93 CRC32 crc = new CRC32(); 94 crc.update(1); 95 // System.out.print("value of crc"+crc.getValue()); 96 // Ran JDK and discovered that the value of the CRC should be 97 // 2768625435 98 assertEquals("update(1) failed to update the checksum to the correct value ", 99 2768625435L, crc.getValue()); 100 101 crc.reset(); 102 crc.update(Integer.MAX_VALUE); 103 // System.out.print("value of crc " + crc.getValue()); 104 // Ran JDK and discovered that the value of the CRC should be 105 // 4278190080 106 assertEquals("update(max) failed to update the checksum to the correct value ", 107 4278190080L, crc.getValue()); 108 109 crc.reset(); 110 crc.update(Integer.MIN_VALUE); 111 // System.out.print("value of crc " + crc.getValue()); 112 // Ran JDK and discovered that the value of the CRC should be 113 // 3523407757 114 assertEquals("update(min) failed to update the checksum to the correct value ", 115 3523407757L, crc.getValue()); 116 } 117 118 /** 119 * java.util.zip.CRC32#update(byte[]) 120 */ 121 public void test_update$B() { 122 // test methods of java.util.zip.crc32.update(byte[]) 123 byte byteArray[] = { 1, 2 }; 124 CRC32 crc = new CRC32(); 125 crc.update(byteArray); 126 // System.out.print("value of crc"+crc.getValue()); 127 // Ran JDK and discovered that the value of the CRC should be 128 // 3066839698 129 assertEquals("update(byte[]) failed to update the checksum to the correct value ", 130 3066839698L, crc.getValue()); 131 132 crc.reset(); 133 byte byteEmpty[] = new byte[10000]; 134 crc.update(byteEmpty); 135 // System.out.print("value of crc"+crc.getValue()); 136 // Ran JDK and discovered that the value of the CRC should be 137 // 1295764014 138 assertEquals("update(byte[]) failed to update the checksum to the correct value ", 139 1295764014L, crc.getValue()); 140 } 141 142 /** 143 * java.util.zip.CRC32#update(byte[], int, int) 144 */ 145 public void test_update$BII() { 146 // test methods of java.util.zip.update(byte[],int,int) 147 byte[] byteArray = { 1, 2, 3 }; 148 CRC32 crc = new CRC32(); 149 int off = 2;// accessing the 2nd element of byteArray 150 int len = 1; 151 int lenError = 3; 152 int offError = 4; 153 crc.update(byteArray, off, len); 154 // System.out.print("value of crc"+crc.getValue()); 155 // Ran JDK and discovered that the value of the CRC should be 156 // 1259060791 157 assertEquals("update(byte[],int,int) failed to update the checksum to the correct value ", 158 1259060791L, crc.getValue()); 159 int r = 0; 160 try { 161 crc.update(byteArray, off, lenError); 162 } catch (ArrayIndexOutOfBoundsException e) { 163 r = 1; 164 } 165 assertEquals("update(byte[],int,int) failed b/c lenError>byte[].length-off", 166 1, r); 167 168 try { 169 crc.update(byteArray, offError, len); 170 } catch (ArrayIndexOutOfBoundsException e) { 171 r = 2; 172 } 173 assertEquals("update(byte[],int,int) failed b/c offError>byte[].length", 174 2, r); 175 } 176 177 @Override 178 protected void setUp() { 179 180 } 181 182 @Override 183 protected void tearDown() { 184 } 185 186 } 187