1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 package com.sun.star.lib.uno.protocols.urp; 25 26 import org.junit.Test; 27 import static org.junit.Assert.*; 28 29 public final class Cache_Test { test0()30 @Test public void test0() { 31 Cache c = new Cache(0); 32 boolean[] f = new boolean[1]; 33 int i; 34 i = c.add(f, "a"); 35 assertTrue(i == Cache.NOT_CACHED && !f[0]); 36 i = c.add(f, "a"); 37 assertTrue(i == Cache.NOT_CACHED && !f[0]); 38 i = c.add(f, "b"); 39 assertTrue(i == Cache.NOT_CACHED && !f[0]); 40 i = c.add(f, "a"); 41 assertTrue(i == Cache.NOT_CACHED && !f[0]); 42 } 43 test1()44 @Test public void test1() { 45 Cache c = new Cache(1); 46 boolean[] f = new boolean[1]; 47 int i; 48 i = c.add(f, "a"); 49 assertTrue(i == 0 && !f[0]); 50 i = c.add(f, "a"); 51 assertTrue(i == 0 && f[0]); 52 i = c.add(f, "b"); 53 assertTrue(i == 0 && !f[0]); 54 i = c.add(f, "b"); 55 assertTrue(i == 0 && f[0]); 56 i = c.add(f, "a"); 57 assertTrue(i == 0 && !f[0]); 58 } 59 test2()60 @Test public void test2() { 61 Cache c = new Cache(2); 62 boolean[] f = new boolean[1]; 63 int i; 64 i = c.add(f, "a"); 65 assertTrue(i == 0 && !f[0]); 66 i = c.add(f, "a"); 67 assertTrue(i == 0 && f[0]); 68 i = c.add(f, "b"); 69 assertTrue(i == 1 && !f[0]); 70 i = c.add(f, "b"); 71 assertTrue(i == 1 && f[0]); 72 i = c.add(f, "a"); 73 assertTrue(i == 0 && f[0]); 74 i = c.add(f, "c"); 75 assertTrue(i == 1 && !f[0]); 76 i = c.add(f, "b"); 77 assertTrue(i == 0 && !f[0]); 78 } 79 test3()80 @Test public void test3() { 81 Cache c = new Cache(3); 82 boolean[] f = new boolean[1]; 83 int i; 84 i = c.add(f, "a"); 85 assertTrue(i == 0 && !f[0]); 86 i = c.add(f, "a"); 87 assertTrue(i == 0 && f[0]); 88 i = c.add(f, "b"); 89 assertTrue(i == 1 && !f[0]); 90 i = c.add(f, "a"); 91 assertTrue(i == 0 && f[0]); 92 i = c.add(f, "c"); 93 assertTrue(i == 2 && !f[0]); 94 i = c.add(f, "d"); 95 assertTrue(i == 1 && !f[0]); 96 i = c.add(f, "d"); 97 assertTrue(i == 1 && f[0]); 98 } 99 testNothingLostFromLruList()100 @Test public void testNothingLostFromLruList() { 101 // Regardless in what order arbitrary values from 0, ..., 3 are inserted 102 // into a size-4 cache, afterwards adding -1, ..., -4 must return each 103 // possible index in the range from 0, ..., 3 exactly once (so their sum 104 // must be 6); this code systematically tests all such arbitrary ways up 105 // to length 8 (the code arguably violates recommendations for writing 106 // good tests, but actually helped track down an error in the Cache 107 // implementation): 108 int[] a = new int[8]; 109 for (int i = 0; i < a.length; ++i) { 110 for (int j = 0; j < i; ++j) { 111 a[j] = 0; 112 } 113 for (;;) { 114 Cache c = new Cache(4); 115 for (int k = 0; k < i; ++k) { 116 c.add(new boolean[1], a[k]); 117 } 118 assertEquals( 119 6, 120 (c.add(new boolean[1], -1) + c.add(new boolean[1], -2) + 121 c.add(new boolean[1], -3) + c.add(new boolean[1], -4))); 122 int j = i - 1; 123 while (j >= 0 && a[j] == 3) { 124 --j; 125 } 126 if (j < 0) { 127 break; 128 } 129 ++a[j]; 130 for (int k = j + 1; k < i; ++k) { 131 a[k] = 0; 132 } 133 } 134 } 135 } 136 } 137