Import atomix/{storage,utils}
[controller.git] / third-party / atomix / storage / src / test / java / io / atomix / storage / journal / index / SparseJournalIndexTest.java
1 /*
2  * Copyright 2018-present Open Networking 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 package io.atomix.storage.journal.index;
17
18 import org.junit.Test;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertNull;
22
23 /**
24  * Sparse journal index test.
25  */
26 public class SparseJournalIndexTest {
27   @Test
28   public void testSparseJournalIndex() throws Exception {
29     JournalIndex index = new SparseJournalIndex(.2);
30     assertNull(index.lookup(1));
31     index.index(1, 2);
32     assertNull(index.lookup(1));
33     index.index(2, 4);
34     index.index(3, 6);
35     index.index(4, 8);
36     index.index(5, 10);
37     assertEquals(5, index.lookup(5).index());
38     assertEquals(10, index.lookup(5).position());
39     index.index(6, 12);
40     index.index(7, 14);
41     index.index(8, 16);
42     assertEquals(5, index.lookup(8).index());
43     assertEquals(10, index.lookup(8).position());
44     index.index(9, 18);
45     index.index(10, 20);
46     assertEquals(10, index.lookup(10).index());
47     assertEquals(20, index.lookup(10).position());
48     index.truncate(8);
49     assertEquals(5, index.lookup(8).index());
50     assertEquals(10, index.lookup(8).position());
51     assertEquals(5, index.lookup(10).index());
52     assertEquals(10, index.lookup(10).position());
53     index.truncate(4);
54     assertNull(index.lookup(4));
55     assertNull(index.lookup(8));
56
57     index = new SparseJournalIndex(.2);
58     assertNull(index.lookup(100));
59     index.index(101, 2);
60     assertNull(index.lookup(1));
61     index.index(102, 4);
62     index.index(103, 6);
63     index.index(104, 8);
64     index.index(105, 10);
65     assertEquals(105, index.lookup(105).index());
66     assertEquals(10, index.lookup(105).position());
67     index.index(106, 12);
68     index.index(107, 14);
69     index.index(108, 16);
70     assertEquals(105, index.lookup(108).index());
71     assertEquals(10, index.lookup(108).position());
72     index.index(109, 18);
73     index.index(110, 20);
74     assertEquals(110, index.lookup(110).index());
75     assertEquals(20, index.lookup(110).position());
76     index.truncate(108);
77     assertEquals(105, index.lookup(108).index());
78     assertEquals(10, index.lookup(108).position());
79     assertEquals(105, index.lookup(110).index());
80     assertEquals(10, index.lookup(110).position());
81     index.truncate(104);
82     assertNull(index.lookup(104));
83     assertNull(index.lookup(108));
84   }
85 }