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.math.BigInteger;
20  
21  import org.apache.asn1new.util.IntegerDecoder;
22  
23  import junit.framework.Assert;
24  import junit.framework.TestCase;
25  
26  /***
27   * This class is used to test the Value class 
28   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
29   */
30  public class ValueTest extends TestCase {
31  
32      /***
33       * Test the getNbBytes method
34       */
35      public void testValueGetNbBytes() 
36      {
37          Assert.assertEquals(1, Value.getNbBytes( 0 ) );
38          Assert.assertEquals(1, Value.getNbBytes( 1 ) );
39          Assert.assertEquals(2, Value.getNbBytes( 255 ) );
40          Assert.assertEquals(2, Value.getNbBytes( 256 ) );
41          Assert.assertEquals(3, Value.getNbBytes( 65535 ) );
42          Assert.assertEquals(3, Value.getNbBytes( 65536 ) );
43          Assert.assertEquals(4, Value.getNbBytes( 16777215 ) );
44          Assert.assertEquals(4, Value.getNbBytes( 16777216 ) );
45          Assert.assertEquals(1, Value.getNbBytes( -1 ) );
46      }
47      
48      public void testEncodeInt2Bytes()
49      {
50          byte[] encoded = Value.getBytes( 128 );
51          
52          Assert.assertEquals( 0x00, encoded[0] );
53          Assert.assertEquals( (byte)0x80, encoded[1] );
54  
55          encoded = Value.getBytes( -27066 );
56          
57          Assert.assertEquals( (byte)0x96, (byte)encoded[0] );
58          Assert.assertEquals( 0x46, encoded[1] );
59          
60      }
61  
62      public void testEncodeInt3Bytes()
63      {
64  
65          byte[] encoded = Value.getBytes( 32787 );
66          
67          Assert.assertEquals( 0x00, encoded[0] );
68          Assert.assertEquals( (byte)0x80, encoded[1] );
69          Assert.assertEquals( (byte)0x13, encoded[2] );
70      }
71      
72      public void testEncodeInt()
73      {
74          byte[] encoded = null;
75          int[] testedInt = new int[] { Integer.MIN_VALUE, -2147483647, -16777216, -16777215, -8388608, 
76                  -8388607, -65536, -65535, -32768, -32767, -256, -255, -128, -127, -1, 0,
77                  1, 127, 128, 255, 256, 32767, 32768, 65535, 65536, 8388607, 8388608, 16777215, 16777216,
78                  Integer.MAX_VALUE};
79          
80          for ( int i = 0; i < testedInt.length; i++)
81          {
82              encoded = Value.getBytes( testedInt[i] );
83              
84              int value = new BigInteger( encoded ).intValue(); 
85              
86              Assert.assertEquals(testedInt[i], value );
87          }
88      }
89      
90      public void testDecodeInt() throws Exception
91      {
92          byte[] encoded = null;
93          int[] testedInt = new int[] { Integer.MIN_VALUE, -2147483647, -16777216, -16777215, -8388608, 
94                  -8388607, -65536, -65535, -32768, -32767, -256, -255, -128, -127, -1, 0,
95                  1, 127, 128, 255, 256, 32767, 32768, 65535, 65536, 8388607, 8388608, 16777215, 16777216,
96                  Integer.MAX_VALUE};
97          
98          for ( int i = 0; i < testedInt.length; i++)
99          {
100             encoded = new BigInteger( Integer.toString( testedInt[i] ) ).toByteArray();
101             
102             int value = IntegerDecoder.parse( new Value( encoded ) ); 
103             
104             Assert.assertEquals(testedInt[i], value );
105         }
106     }
107 }