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.comparator;
21  
22  
23  import static org.junit.Assert.assertEquals;
24  
25  import org.apache.directory.mavibot.btree.comparator.ByteArrayComparator;
26  import org.junit.Test;
27  
28  
29  /**
30   * Test the ByteArrayComparator class
31   * 
32   * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
33   */
34  public class ByteArrayComparatorTest
35  {
36      @Test
37      public void testByteArrayComparator()
38      {
39          ByteArrayComparator comparator = new ByteArrayComparator();
40  
41          // Check equality
42          assertEquals( 0, comparator.compare( null, null ) );
43          assertEquals( 0, comparator.compare( new byte[]
44              {}, new byte[]
45              {} ) );
46          assertEquals( 0, comparator.compare( new byte[]
47              { 0x01, 0x02 }, new byte[]
48              { 0x01, 0x02 } ) );
49  
50          // The first byte[] is > the second
51          assertEquals( 1, comparator.compare( new byte[]
52              {}, null ) );
53          assertEquals( 1, comparator.compare( new byte[]
54              { 0x01 }, null ) );
55          assertEquals( 1, comparator.compare( new byte[]
56              { 0x01, 0x02 }, new byte[]
57              { 0x01, 0x01 } ) );
58          assertEquals( 1, comparator.compare( new byte[]
59              { 0x01, 0x02, 0x01 }, new byte[]
60              { 0x01, 0x02 } ) );
61          assertEquals( 1, comparator.compare( new byte[]
62              { 0x01, 0x02 }, new byte[]
63              { 0x01, 0x01, 0x02 } ) );
64  
65          // The first byte[] is < the second
66          assertEquals( -1, comparator.compare( null, new byte[]
67              {} ) );
68          assertEquals( -1, comparator.compare( null, new byte[]
69              { 0x01, 0x02 } ) );
70          assertEquals( -1, comparator.compare( null, new byte[]
71              { ( byte ) 0xFF, 0x02 } ) );
72          assertEquals( -1, comparator.compare( new byte[]
73              {}, new byte[]
74              { 0x01, 0x02 } ) );
75          assertEquals( -1, comparator.compare( new byte[]
76              {}, new byte[]
77              { ( byte ) 0xFF, 0x02 } ) );
78          assertEquals( -1, comparator.compare( new byte[]
79              { ( byte ) 0xFF, 0x01 }, new byte[]
80              { 0x01, 0x01, 0x02 } ) );
81          byte[] array = new byte[3];
82          array[0] = 0x01;
83          array[1] = 0x02;
84          assertEquals( -1, comparator.compare( new byte[]
85              { 0x01, 0x02 }, array ) );
86  
87      }
88  }