-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP Add
NbtCapabilities.rootTagTypes
- Loading branch information
1 parent
c71a305
commit 8ff08cb
Showing
11 changed files
with
139 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package net.benwoodworth.knbt.internal | ||
|
||
internal class NbtTagTypeSet( | ||
elements: Collection<NbtTagType> | ||
) { | ||
/** | ||
* Bits indicating whether each [NbtTagType] in this set, with `1` indicating that it is contained, and | ||
* [NbtTagType.bit] assigning bit positions. | ||
*/ | ||
private val elementBits: Int = | ||
elements.fold(0) { bits, tagType -> bits or tagType.bit } | ||
|
||
private val NbtTagType.bit: Int | ||
get() = 1 shl id.toInt() | ||
|
||
operator fun contains(type: NbtTagType): Boolean = | ||
(elementBits and type.bit) != 0 | ||
|
||
override fun equals(other: Any?): Boolean = | ||
this === other || (other is NbtTagTypeSet && elementBits == other.elementBits) | ||
|
||
override fun hashCode(): Int = elementBits | ||
|
||
override fun toString(): String = | ||
NbtTagType.entries | ||
.filter { it in this } | ||
.joinToString(prefix = "[", postfix = "]") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package net.benwoodworth.knbt.internal | ||
|
||
import com.benwoodworth.parameterize.parameter | ||
import net.benwoodworth.knbt.test.assume | ||
import net.benwoodworth.knbt.test.parameterizeTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertNotEquals | ||
|
||
class NbtTagTypeSetTest { | ||
private class TestCase( | ||
private val description: String, | ||
val entries: List<NbtTagType> | ||
) { | ||
override fun toString(): String = "$description: $entries" | ||
} | ||
|
||
private val testCases = buildList { | ||
add(TestCase("All", NbtTagType.entries)) | ||
add(TestCase("None", NbtTagType.entries)) | ||
|
||
NbtTagType.entries.forEach { tagType -> | ||
add(TestCase("Only $tagType", listOf(tagType))) | ||
} | ||
|
||
NbtTagType.entries.forEach { tagType -> | ||
add(TestCase("All except $tagType", NbtTagType.entries - tagType)) | ||
} | ||
} | ||
|
||
@Test | ||
fun contains_should_be_correct() = parameterizeTest { | ||
val testCase by parameter(testCases) | ||
val element by parameter(NbtTagType.entries) | ||
|
||
val set = NbtTagTypeSet(testCase.entries) | ||
|
||
assertEquals(element in testCase.entries, element in set, "$element in $set") | ||
} | ||
|
||
@Test | ||
fun equals_should_be_true_for_same_entries() = parameterizeTest { | ||
val testCase by parameter(testCases) | ||
val otherTestCase by parameter(testCases) | ||
assume(testCase.entries.toSet() == otherTestCase.entries.toSet()) | ||
|
||
val set = NbtTagTypeSet(testCase.entries) | ||
val otherSet = NbtTagTypeSet(testCase.entries) | ||
|
||
assertEquals(set, otherSet) | ||
} | ||
|
||
@Test | ||
fun equals_should_be_false_for_different_entries() = parameterizeTest { | ||
val testCase by parameter(testCases) | ||
val otherTestCase by parameter(testCases) | ||
assume(testCase.entries.toSet() != otherTestCase.entries.toSet()) | ||
|
||
val set = NbtTagTypeSet(testCase.entries) | ||
val otherSet = NbtTagTypeSet(otherTestCase.entries) | ||
|
||
assertNotEquals(set, otherSet) | ||
} | ||
|
||
@Test | ||
fun hash_code_should_be_the_same_if_equal() = parameterizeTest { | ||
val testCase by parameter(testCases) | ||
val otherTestCase by parameter(testCases) | ||
|
||
val set = NbtTagTypeSet(testCase.entries) | ||
val otherSet = NbtTagTypeSet(otherTestCase.entries) | ||
assume(set == otherSet) | ||
|
||
assertEquals(set.hashCode(), otherSet.hashCode()) | ||
} | ||
|
||
@Test | ||
fun hash_code_should_not_be_the_same_if_unequal() = parameterizeTest { | ||
val testCase by parameter(testCases) | ||
val otherTestCase by parameter(testCases) | ||
|
||
val set = NbtTagTypeSet(testCase.entries) | ||
val otherSet = NbtTagTypeSet(otherTestCase.entries) | ||
assume(set != otherSet) | ||
|
||
assertNotEquals(set.hashCode(), otherSet.hashCode()) | ||
} | ||
|
||
@Test | ||
fun to_string_should_be_the_same_as_a_set() = parameterizeTest { | ||
val testCase by parameter(testCases) | ||
|
||
assertEquals( | ||
LinkedHashSet(testCase.entries.sortedBy { it.id }).toString(), | ||
NbtTagTypeSet(testCase.entries).toString() | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters