View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.directory.mavibot.btree.serializer;
21  
22  
23  import static org.junit.Assert.assertEquals;
24  
25  import java.io.IOException;
26  
27  import org.junit.Test;
28  
29  
30  /**
31   * Test the ShortSerializer class
32   *
33   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34   */
35  public class ShortSerializerTest
36  {
37      @Test
38      public void testShortSerializer() throws IOException
39      {
40          short value = 0x0000;
41          byte[] result = ShortSerializer.serialize( value );
42  
43          assertEquals( ( byte ) 0x00, result[1] );
44          assertEquals( ( byte ) 0x00, result[0] );
45  
46          assertEquals( value, ShortSerializer.INSTANCE.deserialize( new BufferHandler( result ) ).shortValue() );
47  
48          // ------------------------------------------------------------------
49          value = 0x0001;
50          result = ShortSerializer.serialize( value );
51  
52          assertEquals( ( byte ) 0x01, result[1] );
53          assertEquals( ( byte ) 0x00, result[0] );
54  
55          assertEquals( value, ShortSerializer.INSTANCE.deserialize( new BufferHandler( result ) ).shortValue() );
56  
57          // ------------------------------------------------------------------
58          value = 0x00FF;
59          result = ShortSerializer.serialize( value );
60  
61          assertEquals( ( byte ) 0xFF, result[1] );
62          assertEquals( ( byte ) 0x00, result[0] );
63  
64          assertEquals( value, ShortSerializer.INSTANCE.deserialize( new BufferHandler( result ) ).shortValue() );
65  
66          // ------------------------------------------------------------------
67          value = 0x0100;
68          result = ShortSerializer.serialize( value );
69  
70          assertEquals( ( byte ) 0x00, result[1] );
71          assertEquals( ( byte ) 0x01, result[0] );
72  
73          assertEquals( value, ShortSerializer.INSTANCE.deserialize( new BufferHandler( result ) ).shortValue() );
74  
75          // ------------------------------------------------------------------
76          value = 0x7FFF;
77          result = ShortSerializer.serialize( value );
78  
79          assertEquals( ( byte ) 0xFF, result[1] );
80          assertEquals( ( byte ) 0x7F, result[0] );
81  
82          assertEquals( value, ShortSerializer.INSTANCE.deserialize( new BufferHandler( result ) ).shortValue() );
83  
84          // ------------------------------------------------------------------
85          value = ( short ) 0x8000;
86          result = ShortSerializer.serialize( value );
87  
88          assertEquals( ( byte ) 0x00, result[1] );
89          assertEquals( ( byte ) 0x80, result[0] );
90  
91          assertEquals( value, ShortSerializer.INSTANCE.deserialize( new BufferHandler( result ) ).shortValue() );
92  
93          // ------------------------------------------------------------------
94          value = ( short ) 0xFFFF;
95          result = ShortSerializer.serialize( value );
96  
97          assertEquals( ( byte ) 0xFF, result[1] );
98          assertEquals( ( byte ) 0xFF, result[0] );
99  
100         assertEquals( value, ShortSerializer.INSTANCE.deserialize( new BufferHandler( result ) ).shortValue() );
101     }
102 }