|
| 1 | +/* |
| 2 | + * Copyright (c) 2019 Works Applications Co., Ltd. |
| 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 | + |
| 17 | +package com.worksap.nlp.dartsclone; |
| 18 | + |
| 19 | +import java.nio.charset.StandardCharsets; |
| 20 | + |
| 21 | +import static org.junit.Assert.assertEquals; |
| 22 | +import static org.junit.Assert.assertFalse; |
| 23 | +import static org.junit.Assert.assertNotEquals; |
| 24 | +import static org.junit.Assert.assertTrue; |
| 25 | + |
| 26 | +import org.junit.Before; |
| 27 | +import org.junit.Rule; |
| 28 | +import org.junit.Test; |
| 29 | +import org.junit.rules.TemporaryFolder; |
| 30 | + |
| 31 | +public class TraverseTest { |
| 32 | + |
| 33 | + DoubleArray dic; |
| 34 | + |
| 35 | + static final byte[] A = "a".getBytes(StandardCharsets.UTF_8); |
| 36 | + static final byte[] AB = "ab".getBytes(StandardCharsets.UTF_8); |
| 37 | + static final byte[] AC = "ac".getBytes(StandardCharsets.UTF_8); |
| 38 | + static final byte[] B = "b".getBytes(StandardCharsets.UTF_8); |
| 39 | + static final byte[] C = "c".getBytes(StandardCharsets.UTF_8); |
| 40 | + static final byte[] CD = "cd".getBytes(StandardCharsets.UTF_8); |
| 41 | + |
| 42 | + @Before |
| 43 | + public void setUp() { |
| 44 | + byte[][] keys = new byte[][] { A, AB, CD }; |
| 45 | + dic = new DoubleArray(); |
| 46 | + dic.build(keys, null, null); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void traverse() { |
| 51 | + DoubleArray.TraverseResult r = dic.traverse(A, 0, 0); |
| 52 | + assertTrue(r.result >= 0); |
| 53 | + assertEquals(1, r.offset); |
| 54 | + DoubleArray.TraverseResult r2 = dic.traverse(B, 0, r.nodePosition); |
| 55 | + assertTrue(r2.result >= 0); |
| 56 | + assertEquals(1, r2.offset); |
| 57 | + DoubleArray.TraverseResult r3 = dic.traverse(AB, 1, r.nodePosition); |
| 58 | + assertTrue(r3.result >= 0); |
| 59 | + assertEquals(2, r3.offset); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void traverseNotMatchWithEnd() { |
| 64 | + DoubleArray.TraverseResult r = dic.traverse(C, 0, 0); |
| 65 | + assertEquals(-1, r.result); |
| 66 | + assertEquals(1, r.offset); |
| 67 | + DoubleArray.TraverseResult r2 = dic.traverse(CD, 1, r.nodePosition); |
| 68 | + assertTrue(r2.result >= 0); |
| 69 | + assertEquals(2, r2.offset); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void traverseNotMatchMiddle() { |
| 74 | + DoubleArray.TraverseResult r = dic.traverse(AC, 0, 0); |
| 75 | + assertEquals(-2, r.result); |
| 76 | + assertEquals(1, r.offset); |
| 77 | + DoubleArray.TraverseResult r2 = dic.traverse(B, 0, r.nodePosition); |
| 78 | + assertTrue(r2.result >= 0); |
| 79 | + assertEquals(1, r2.offset); |
| 80 | + } |
| 81 | +} |
0 commit comments