|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * The Universal Permissive License (UPL), Version 1.0 |
| 6 | + * |
| 7 | + * Subject to the condition set forth below, permission is hereby granted to any |
| 8 | + * person obtaining a copy of this software, associated documentation and/or |
| 9 | + * data (collectively the "Software"), free of charge and under any and all |
| 10 | + * copyright rights in the Software, and any and all patent rights owned or |
| 11 | + * freely licensable by each licensor hereunder covering either (i) the |
| 12 | + * unmodified Software as contributed to or provided by such licensor, or (ii) |
| 13 | + * the Larger Works (as defined below), to deal in both |
| 14 | + * |
| 15 | + * (a) the Software, and |
| 16 | + * |
| 17 | + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 18 | + * one is included with the Software each a "Larger Work" to which the Software |
| 19 | + * is contributed by such licensors), |
| 20 | + * |
| 21 | + * without restriction, including without limitation the rights to copy, create |
| 22 | + * derivative works of, display, perform, and distribute the Software and make, |
| 23 | + * use, sell, offer for sale, import, export, have made, and have sold the |
| 24 | + * Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 25 | + * either these or other terms. |
| 26 | + * |
| 27 | + * This license is subject to the following condition: |
| 28 | + * |
| 29 | + * The above copyright notice and either this complete permission notice or at a |
| 30 | + * minimum a reference to the UPL must be included in all copies or substantial |
| 31 | + * portions of the Software. |
| 32 | + * |
| 33 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 34 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 35 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 36 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 37 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 38 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 39 | + * SOFTWARE. |
| 40 | + */ |
| 41 | +package com.oracle.graal.python.builtins.modules; |
| 42 | + |
| 43 | +import static org.junit.Assert.assertEquals; |
| 44 | +import static org.junit.Assert.assertNull; |
| 45 | +import static org.junit.Assert.fail; |
| 46 | + |
| 47 | +import org.junit.After; |
| 48 | +import org.junit.Before; |
| 49 | +import org.junit.Test; |
| 50 | + |
| 51 | +import com.oracle.graal.python.builtins.objects.contextvars.Hamt; |
| 52 | +import com.oracle.graal.python.test.PythonTests; |
| 53 | + |
| 54 | +public class HamtTests { |
| 55 | + @Before |
| 56 | + public void enter() { |
| 57 | + PythonTests.enterContext(); |
| 58 | + } |
| 59 | + |
| 60 | + @After |
| 61 | + public void close() { |
| 62 | + PythonTests.closeContext(); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testHamtConstructed() { |
| 67 | + Hamt hamt = new Hamt(); |
| 68 | + assertEquals("null\n", hamt.dump()); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testHamtSetLookup() { |
| 73 | + Hamt hamt = new Hamt(); |
| 74 | + hamt = hamt.withEntry(new Hamt.Entry(1, 1, 1)); |
| 75 | + assertEquals(1, hamt.lookup(1, 1)); |
| 76 | + hamt = hamt.withEntry(new Hamt.Entry(0, 0, 0)); |
| 77 | + assertEquals(0, hamt.lookup(0, 0)); |
| 78 | + assertEquals(1, hamt.lookup(1, 1)); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testBitmapExtremes() { |
| 83 | + Hamt hamt = new Hamt(); |
| 84 | + hamt = hamt.withEntry(new Hamt.Entry(1, 0, 1)); |
| 85 | + hamt = hamt.withEntry(new Hamt.Entry(2, 31, 2)); |
| 86 | + assertEquals(2, hamt.lookup(2, 31)); |
| 87 | + hamt = new Hamt(); |
| 88 | + hamt = hamt.withEntry(new Hamt.Entry(1, 31, 1)); |
| 89 | + hamt = hamt.withEntry(new Hamt.Entry(2, 0, 2)); |
| 90 | + assertEquals(2, hamt.lookup(2, 0)); |
| 91 | + assertEquals(1, hamt.lookup(1, 31)); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testSequentialNumbers() { |
| 96 | + int limit = 700; |
| 97 | + Hamt hamt = new Hamt(); |
| 98 | + for (int i = 0; i < limit; ++i) { |
| 99 | + hamt = hamt.withEntry(new Hamt.Entry(i, i, i)); |
| 100 | + for (int j = 0; j < limit; ++j) { |
| 101 | + assertEquals(j <= i ? j : null, hamt.lookup(j, j)); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testLotsOfEntries() { |
| 108 | + Hamt hamt = new Hamt(); |
| 109 | + for (int i = 0; i < 100000; ++i) { |
| 110 | + hamt = hamt.withEntry(new Hamt.Entry(i, i * 3, i * 2)); |
| 111 | + } |
| 112 | + for (int i = 0; i < 100000; ++i) { |
| 113 | + assertEquals(i * 2, hamt.lookup(i, i * 3)); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void addAndRemoveEntries() { |
| 119 | + int limit = 700; |
| 120 | + Hamt hamt = new Hamt(); |
| 121 | + for (int i = 0; i < limit; ++i) { |
| 122 | + hamt = hamt.withEntry(new Hamt.Entry(i, i * 3, i)); |
| 123 | + for (int j = 0; j < limit; ++j) { |
| 124 | + assertNull(hamt.without(j, j * 3).lookup(j, j * 3)); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void addAndRemoveAllEntries() { |
| 131 | + int limit = 70000; |
| 132 | + Hamt hamt = new Hamt(); |
| 133 | + for (int i = 0; i < limit; ++i) { |
| 134 | + hamt = hamt.withEntry(new Hamt.Entry(i, i * 3, i + 1)); |
| 135 | + } |
| 136 | + Hamt fullHamt = hamt; |
| 137 | + for (int i = limit - 1; i >= 0; --i) { |
| 138 | + hamt = hamt.without(i, i * 3); |
| 139 | + if (hamt.lookup(i, i * 3) != null) { |
| 140 | + fail(i + ":" + hamt.dump()); |
| 141 | + } |
| 142 | + } |
| 143 | + assertEquals("null\n", hamt.dump()); |
| 144 | + hamt = fullHamt; |
| 145 | + for (int i = 0; i < limit; ++i) { |
| 146 | + hamt = hamt.without(i, i * 3); |
| 147 | + if (hamt.lookup(i, i * 3) != null) { |
| 148 | + fail(i + ":" + hamt.dump()); |
| 149 | + } |
| 150 | + } |
| 151 | + assertEquals("null\n", hamt.dump()); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + public void largeHashVariance() { |
| 156 | + int limit = 70000; |
| 157 | + Hamt hamt = new Hamt(); |
| 158 | + for (int i = 0; i < limit; ++i) { |
| 159 | + hamt = hamt.withEntry(new Hamt.Entry(i, String.valueOf(i).hashCode(), i + 1)); |
| 160 | + } |
| 161 | + for (int i = 0; i < limit; ++i) { |
| 162 | + assertEquals(i + 1, hamt.lookup(i, String.valueOf(i).hashCode())); |
| 163 | + } |
| 164 | + for (int i = 0; i < limit; ++i) { |
| 165 | + hamt = hamt.without(i, String.valueOf(i).hashCode()); |
| 166 | + } |
| 167 | + assertEquals("null\n", hamt.dump()); |
| 168 | + } |
| 169 | + |
| 170 | +// @Test |
| 171 | + public void measureTimeForSmallHamtAcceses() { |
| 172 | + Hamt hamt = new Hamt().withEntry(new Hamt.Entry(1, 0, 1)).withEntry(new Hamt.Entry(2, 31, 2)); |
| 173 | + long start = System.nanoTime(); |
| 174 | + for (int i = 0; i < 1000000; ++i) { |
| 175 | + Hamt requestContext = hamt.withEntry(new Hamt.Entry(2, 31, 1)); |
| 176 | + requestContext.lookup(2, 31); |
| 177 | + requestContext.lookup(1, 0); |
| 178 | + } |
| 179 | + System.out.println("Took " + (System.nanoTime() - start) + " nanoseconds"); |
| 180 | + |
| 181 | + } |
| 182 | +} |
0 commit comments