1   /*
2    *   Copyright 2005 The Apache Software Foundation
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
15   *
16   */
17  package org.apache.asn1new.ber.tlv;
18  
19  import java.util.Arrays;
20  
21  import junit.framework.Assert;
22  import junit.framework.TestCase;
23  
24  /***
25   * This class is used to test the Length class 
26   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
27   */
28  public class LengthTest extends TestCase {
29  
30      /***
31       * Test the getNbBytes method
32       */
33      public void testLengthGetNbBytes() 
34      {
35          Assert.assertEquals(1, Length.getNbBytes(0));
36          Assert.assertEquals(1, Length.getNbBytes(1));
37          Assert.assertEquals(1, Length.getNbBytes(127));
38          Assert.assertEquals(2, Length.getNbBytes(128));
39          Assert.assertEquals(2, Length.getNbBytes(255));
40          Assert.assertEquals(3, Length.getNbBytes(256));
41          Assert.assertEquals(3, Length.getNbBytes(65535));
42          Assert.assertEquals(4, Length.getNbBytes(65536));
43          Assert.assertEquals(4, Length.getNbBytes(16777215));
44          Assert.assertEquals(5, Length.getNbBytes(16777216));
45          Assert.assertEquals(5, Length.getNbBytes(0xFFFFFFFF));
46      }
47  
48      /***
49       * Test the getBytes method
50       */
51      public void testLengthGetBytes() 
52      {
53          assertTrue(Arrays.equals(new byte[]{0x01}, Length.getBytes(1)));
54          assertTrue(Arrays.equals(new byte[]{0x7F}, Length.getBytes(127)));
55          assertTrue(Arrays.equals(new byte[]{(byte)0x81, (byte)0x80}, Length.getBytes(128)));
56          assertTrue(Arrays.equals(new byte[]{(byte)0x81, (byte)0xFF}, Length.getBytes(255)));
57          assertTrue(Arrays.equals(new byte[]{(byte)0x82, 0x01, 0x00}, Length.getBytes(256)));
58          assertTrue(Arrays.equals(new byte[]{(byte)0x82, (byte)0xFF, (byte)0xFF}, Length.getBytes(65535)));
59          assertTrue(Arrays.equals(new byte[]{(byte)0x83, 0x01, 0x00, 0x00}, Length.getBytes(65536)));
60          assertTrue(Arrays.equals(new byte[]{(byte)0x83, (byte)0xFF, (byte)0xFF, (byte)0xFF}, Length.getBytes(16777215)));
61          assertTrue(Arrays.equals(new byte[]{(byte)0x84, 0x01, 0x00, 0x00, 0x00}, Length.getBytes(16777216)));
62          assertTrue(Arrays.equals(new byte[]{(byte)0x84, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF}, Length.getBytes(0xFFFFFFFF)));
63      }
64  }