Quelle AbstractCompressExpandTest.java
Sprache: JAVA
/* * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
staticint testCompress(int i, int mask) { int result = 0; int rpos = 0; while (mask != 0) { if ((mask & 1) != 0) {
result |= (i & 1) << rpos;
rpos++; // conditional increment
}
i >>>= 1; // unconditional shift-out
mask >>>= 1;
} return result;
}
staticint testExpand(int i, int mask) { int result = 0; int rpos = 0; while (mask != 0) { if ((mask & 1) != 0) {
result |= (i & 1) << rpos;
i >>>= 1; // conditional shift-out
}
rpos++; // unconditional increment
mask >>>= 1;
} return result;
}
staticlong testCompress(long i, long mask) { long result = 0; int rpos = 0; while (mask != 0) { if ((mask & 1) != 0) {
result |= (i & 1) << rpos++;
}
i >>>= 1;
mask >>>= 1;
} return result;
}
staticlong testExpand(long i, long mask) { long result = 0; int rpos = 0; while (mask != 0) { if ((mask & 1) != 0) {
result |= (i & 1) << rpos;
i >>>= 1;
}
rpos++;
mask >>>= 1;
} return result;
}
abstractint actualCompress(int i, int mask);
abstractint actualExpand(int i, int mask);
abstractint expectedCompress(int i, int mask);
abstractint expectedExpand(int i, int mask);
abstractlong actualCompress(long i, long mask);
abstractlong actualExpand(long i, long mask);
abstractlong expectedCompress(long i, long mask);
abstractlong expectedExpand(long i, long mask);
staticint SIZE = 1024;
<T> Supplier<T> supplierWithToString(Supplier<T> s, String name) { returnnew Supplier<>() {
@Override public T get() { return s.get();
}
@Override public String toString() { return name;
}
};
}
for (int i : values) { for (int m : masks) { int actual = actualCompress(i, m); int expected = expectedCompress(i, m); if (actual != expected) {
print(i, m, actual, expected);
} Assert.assertEquals(actual, expected);
}
}
}
for (int i : values) { for (int m : masks) { int actual = actualExpand(i, m); int expected = expectedExpand(i, m); if (actual != expected) {
print(i, m, actual, expected);
} Assert.assertEquals(actual, expected);
}
}
}
void assertContiguousMask(int i, int pos, int mask) { Assert.assertEquals(actualCompress(i, mask), (i & mask) >>> pos); Assert.assertEquals(actualExpand(i, mask), (i << pos) & mask);
}
for (long i : values) { for (long m : masks) { long actual = actualCompress(i, m); long expected = expectedCompress(i, m); if (actual != expected) {
print(i, m, actual, expected);
} Assert.assertEquals(actual, expected);
}
}
}
for (long i : values) { for (long m : masks) { long actual = actualExpand(i, m); long expected = expectedExpand(i, m); if (actual != expected) {
print(i, m, actual, expected);
} Assert.assertEquals(actual, expected);
}
}
}
for (long i : values) { for (long m : masks) {
{ long a = actualCompress(actualExpand(i, m), m); Assert.assertEquals(a, normalizeCompressedValue(i, m));
long b = actualCompress(actualExpand(i, ~m), ~m); Assert.assertEquals(b, normalizeCompressedValue(i, ~m));
}
{ long a = actualExpand(actualCompress(i, m), m); // Clear unset mask bits Assert.assertEquals(a, i & m);
long b = actualExpand(actualCompress(i, ~m), ~m); Assert.assertEquals(a & b, 0); Assert.assertEquals(a | b, i);
}
}
}
}
void assertContiguousMask(long i, int pos, long mask) { Assert.assertEquals(actualCompress(i, mask), (i & mask) >>> pos); Assert.assertEquals(actualExpand(i, mask), (i << pos) & mask);
}
staticint normalizeCompressedValue(int i, int mask) { int mbc = Integer.bitCount(mask); if (mbc != 32) { return i & ((1 << mbc) - 1);
} else { return i;
}
}
staticlong normalizeCompressedValue(long i, long mask) { int mbc = Long.bitCount(mask); if (mbc != 64) { return i & ((1L << mbc) - 1);
} else { return i;
}
}
staticvoid print(int i, int m, int actual, int expected) {
System.out.println(String.format("i = %s", Integer.toBinaryString(i)));
System.out.println(String.format("m = %s", Integer.toBinaryString(m)));
System.out.println(String.format("a = %s", Integer.toBinaryString(actual)));
System.out.println(String.format("e = %s", Integer.toBinaryString(expected)));
}
staticvoid print(long i, long m, long actual, long expected) {
System.out.println(String.format("i = %s", Long.toBinaryString(i)));
System.out.println(String.format("m = %s", Long.toBinaryString(m)));
System.out.println(String.format("a = %s", Long.toBinaryString(actual)));
System.out.println(String.format("e = %s", Long.toBinaryString(expected)));
}
}
¤ Dauer der Verarbeitung: 0.2 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.