-
Notifications
You must be signed in to change notification settings - Fork 362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add partial implementation of java.util.EnumSet #3397
Merged
WojciechMazur
merged 1 commit into
scala-native:main
from
WojciechMazur:javalib/enum-set
Jul 19, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
127 changes: 127 additions & 0 deletions
127
...ts/shared/src/test/scala-3/scala/org/scalanative/testsuite/javalib/util/EnumSetTest.scala
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,127 @@ | ||
package org.scalanative.testsuite.javalib.util | ||
|
||
import java.lang._ | ||
import java.util.EnumSet | ||
|
||
import org.junit.Test | ||
import org.junit.Assert._ | ||
|
||
// Tested only in Scala 3 becouse we cannot create Java enums in Scala 2 | ||
|
||
object EnumSetTest { | ||
enum Value extends java.lang.Enum[Value]: | ||
case A, B, C, D, E, F | ||
} | ||
|
||
class EnumSetTest { | ||
import EnumSetTest.Value | ||
|
||
@Test def noneOf(): Unit = { | ||
val s = EnumSet.noneOf(classOf[Value]) | ||
assertTrue(s.isEmpty()) | ||
assertEquals(0, s.size()) | ||
assertFalse(s.iterator().hasNext()) | ||
} | ||
|
||
@Test def of1(): Unit = { | ||
val s = EnumSet.of(Value.A) | ||
assertFalse(s.isEmpty()) | ||
assertEquals(1, s.size()) | ||
val it = s.iterator() | ||
assertTrue(it.hasNext()) | ||
assertEquals(Value.A, it.next()) | ||
} | ||
|
||
@Test def of2(): Unit = { | ||
val s = EnumSet.of(Value.A, Value.B) | ||
assertFalse(s.isEmpty()) | ||
assertEquals(2, s.size()) | ||
assertTrue(s.contains(Value.A)) | ||
assertTrue(s.contains(Value.B)) | ||
} | ||
|
||
@Test def of3(): Unit = { | ||
val s = EnumSet.of(Value.A, Value.B, Value.C) | ||
assertFalse(s.isEmpty()) | ||
assertEquals(3, s.size()) | ||
assertTrue(s.contains(Value.A)) | ||
assertTrue(s.contains(Value.B)) | ||
assertTrue(s.contains(Value.C)) | ||
} | ||
|
||
@Test def of4(): Unit = { | ||
val s = EnumSet.of(Value.A, Value.B, Value.C, Value.D) | ||
assertFalse(s.isEmpty()) | ||
assertEquals(4, s.size()) | ||
assertTrue(s.contains(Value.A)) | ||
assertTrue(s.contains(Value.B)) | ||
assertTrue(s.contains(Value.C)) | ||
assertTrue(s.contains(Value.D)) | ||
} | ||
|
||
@Test def of5(): Unit = { | ||
val s = EnumSet.of(Value.A, Value.B, Value.C, Value.D, Value.E) | ||
assertFalse(s.isEmpty()) | ||
assertEquals(5, s.size()) | ||
assertTrue(s.contains(Value.A)) | ||
assertTrue(s.contains(Value.B)) | ||
assertTrue(s.contains(Value.C)) | ||
assertTrue(s.contains(Value.D)) | ||
assertTrue(s.contains(Value.E)) | ||
} | ||
|
||
@Test def ofVarArg(): Unit = { | ||
val s = EnumSet.of(Value.A, Value.B, Value.C, Value.D, Value.E, Value.F) | ||
assertFalse(s.isEmpty()) | ||
assertEquals(6, s.size()) | ||
assertTrue(s.contains(Value.A)) | ||
assertTrue(s.contains(Value.B)) | ||
assertTrue(s.contains(Value.C)) | ||
assertTrue(s.contains(Value.D)) | ||
assertTrue(s.contains(Value.E)) | ||
assertTrue(s.contains(Value.F)) | ||
} | ||
|
||
@Test def ofVarArg2(): Unit = { | ||
val s = | ||
EnumSet.of(Value.A, Seq(Value.B, Value.C, Value.D, Value.E, Value.F): _*) | ||
assertFalse(s.isEmpty()) | ||
assertEquals(6, s.size()) | ||
assertTrue(s.contains(Value.A)) | ||
assertTrue(s.contains(Value.B)) | ||
assertTrue(s.contains(Value.C)) | ||
assertTrue(s.contains(Value.D)) | ||
assertTrue(s.contains(Value.E)) | ||
assertTrue(s.contains(Value.F)) | ||
} | ||
|
||
@Test def copyOf(): Unit = { | ||
val s = EnumSet.of(Value.A, Value.B, Value.C) | ||
val c = EnumSet.copyOf(s) | ||
assertNotSame(s, c) | ||
assertEquals(s, c) | ||
} | ||
|
||
@Test def copyOfCollection(): Unit = { | ||
val c: java.util.Collection[Value] = new java.util.LinkedList[Value]() | ||
c.add(Value.A) | ||
c.add(Value.B) | ||
c.add(Value.C) | ||
val s = EnumSet.copyOf(c) | ||
|
||
assertNotSame(s, c) | ||
assertEquals(3, s.size()) | ||
assertTrue(s.contains(Value.A)) | ||
assertTrue(s.contains(Value.B)) | ||
assertTrue(s.contains(Value.C)) | ||
} | ||
|
||
@Test def uniqness(): Unit = { | ||
val s = EnumSet.of(Value.A, Value.A, Value.B, Value.C, Value.B) | ||
assertEquals(3, s.size()) | ||
assertTrue(s.contains(Value.A)) | ||
assertTrue(s.contains(Value.B)) | ||
assertTrue(s.contains(Value.C)) | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this could still be an array where the index corresponds to ordinal, and the max ordinal + 1 is the size of the array? (never shrink, only grow by powers of 2?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, it could have been, but it's not going to be frequently used, and that way we get the Set ops for free. At some point it could be rewriten to be more optimal, but for current usages its good enough, especially since we cannot support the relection based semantics to populate it with all values