Skip to content

Commit

Permalink
DefineListTest: rewrite tests to accept any valid hashCode() function
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Oct 16, 2023
1 parent 2cbd5f9 commit 9cefbdc
Showing 1 changed file with 50 additions and 31 deletions.
81 changes: 50 additions & 31 deletions jme3-core/src/test/java/com/jme3/shader/DefineListTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009-2021 jMonkeyEngine
* Copyright (c) 2009-2023 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -91,7 +91,9 @@ private String generateSource(DefineList dl) {
@Test
public void testSourceInitial() {
DefineList dl = new DefineList(NUM_DEFINES);
assert dl.hashCode() == 0;
for (int id = 0; id < NUM_DEFINES; ++id) {
assert !dl.isSet(id);
}
assert generateSource(dl).equals("");
}

Expand All @@ -100,46 +102,68 @@ public void testSourceBooleanDefine() {
DefineList dl = new DefineList(NUM_DEFINES);

dl.set(BOOL_VAR, true);
assert dl.hashCode() == 1;
for (int id = 0; id < NUM_DEFINES; ++id) {
boolean isBoolVar = (id == BOOL_VAR);
assert dl.isSet(id) == isBoolVar;
}
assert generateSource(dl).equals("#define BOOL_VAR 1\n");

dl.set(BOOL_VAR, false);
assert dl.hashCode() == 0;
for (int id = 0; id < NUM_DEFINES; ++id) {
assert !dl.isSet(id);
}
assert generateSource(dl).equals("");

dl.set(BOOL_VAR, true);
assert dl.hashCode() == 1;
for (int id = 0; id < NUM_DEFINES; ++id) {
boolean isBoolVar = (id == BOOL_VAR);
assert dl.isSet(id) == isBoolVar;
}
assert generateSource(dl).equals("#define BOOL_VAR 1\n");

dl.unset(BOOL_VAR);
assert dl.hashCode() == 0;
for (int id = 0; id < NUM_DEFINES; ++id) {
assert !dl.isSet(id);
}
assert generateSource(dl).equals("");
}

@Test
public void testSourceIntDefine() {
DefineList dl = new DefineList(NUM_DEFINES);

int hashCodeWithInt = 1 << INT_VAR;

dl.set(INT_VAR, 123);
assert dl.hashCode() == hashCodeWithInt;
for (int id = 0; id < NUM_DEFINES; ++id) {
boolean isIntVar = (id == INT_VAR);
assert dl.isSet(id) == isIntVar;
}
assert generateSource(dl).equals("#define INT_VAR 123\n");

dl.set(INT_VAR, 0);
assert dl.hashCode() == hashCodeWithInt;
for (int id = 0; id < NUM_DEFINES; ++id) {
boolean isIntVar = (id == INT_VAR);
assert dl.isSet(id) == isIntVar;
}
assert generateSource(dl).equals("#define INT_VAR 0\n");

dl.set(INT_VAR, -99);
assert dl.hashCode() == hashCodeWithInt;
for (int id = 0; id < NUM_DEFINES; ++id) {
boolean isIntVar = (id == INT_VAR);
assert dl.isSet(id) == isIntVar;
}
assert generateSource(dl).equals("#define INT_VAR -99\n");

dl.set(INT_VAR, Integer.MAX_VALUE);
assert dl.hashCode() == hashCodeWithInt;
for (int id = 0; id < NUM_DEFINES; ++id) {
boolean isIntVar = (id == INT_VAR);
assert dl.isSet(id) == isIntVar;
}
assert generateSource(dl).equals("#define INT_VAR 2147483647\n");

dl.unset(INT_VAR);
assert dl.hashCode() == 0;
for (int id = 0; id < NUM_DEFINES; ++id) {
assert !dl.isSet(id);
}
assert generateSource(dl).equals("");
}

Expand All @@ -148,11 +172,17 @@ public void testSourceFloatDefine() {
DefineList dl = new DefineList(NUM_DEFINES);

dl.set(FLOAT_VAR, 1f);
assert dl.hashCode() == (1 << FLOAT_VAR);
for (int id = 0; id < NUM_DEFINES; ++id) {
boolean isFloatVar = (id == FLOAT_VAR);
assert dl.isSet(id) == isFloatVar;
}
assert generateSource(dl).equals("#define FLOAT_VAR 1.0\n");

dl.set(FLOAT_VAR, 0f);
assert dl.hashCode() == (1 << FLOAT_VAR);
for (int id = 0; id < NUM_DEFINES; ++id) {
boolean isFloatVar = (id == FLOAT_VAR);
assert dl.isSet(id) == isFloatVar;
}
assert generateSource(dl).equals("#define FLOAT_VAR 0.0\n");

dl.set(FLOAT_VAR, -1f);
Expand Down Expand Up @@ -191,49 +221,38 @@ public void testEqualsAndHashCode() {
DefineList dl1 = new DefineList(NUM_DEFINES);
DefineList dl2 = new DefineList(NUM_DEFINES);

assertEquals(0, dl1.hashCode());
assertEquals(0, dl2.hashCode());
assertEquals(dl1.hashCode(), dl2.hashCode());
assertEquals(dl1, dl2);

dl1.set(BOOL_VAR, true);

assertEquals(1, dl1.hashCode());
assertEquals(0, dl2.hashCode());
assertNotEquals(dl1, dl2);

dl2.set(BOOL_VAR, true);

assertEquals(1, dl1.hashCode());
assertEquals(1, dl2.hashCode());
assertEquals(dl1.hashCode(), dl2.hashCode());
assertEquals(dl1, dl2);

dl1.set(INT_VAR, 2);

assertEquals(1 | 2, dl1.hashCode());
assertEquals(1, dl2.hashCode());
assertNotEquals(dl1, dl2);

dl2.set(INT_VAR, 2);

assertEquals(1 | 2, dl1.hashCode());
assertEquals(1 | 2, dl2.hashCode());
assertEquals(dl1.hashCode(), dl2.hashCode());
assertEquals(dl1, dl2);

dl1.set(BOOL_VAR, false);

assertEquals(2, dl1.hashCode());
assertEquals(1 | 2, dl2.hashCode());
assertNotEquals(dl1, dl2);

dl2.unset(BOOL_VAR);

assertEquals(2, dl1.hashCode());
assertEquals(2, dl2.hashCode());
assertEquals(dl1.hashCode(), dl2.hashCode());
assertEquals(dl1, dl2); // unset is the same as false

dl1.unset(BOOL_VAR);
assertEquals(2, dl1.hashCode());
assertEquals(2, dl2.hashCode());
assertEquals(dl1.hashCode(), dl2.hashCode());
assertEquals(dl1, dl2);
}

Expand Down

0 comments on commit 9cefbdc

Please sign in to comment.