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