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