-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
150 additions
and
85 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
66 changes: 66 additions & 0 deletions
66
interface/java_binding/src/main/java/org/khronos/ktx/KtxUtilities.java
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,66 @@ | ||
/* | ||
* Copyright (c) 2024, Khronos Group and Contributors | ||
* Copyright (c) 2021, Shukant Pal and Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.khronos.ktx; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* Package-private utility methods | ||
*/ | ||
class KtxUtilities { | ||
|
||
/** | ||
* Validates the given value as an input swizzle to be set as | ||
* {@link KtxBasisParams#setInputSwizzle(char[])} or | ||
* {@link KtxAstcParams#setInputSwizzle(char[])}.<br> | ||
* <br> | ||
* When the given swizzle is <code>null</code>, then a default swizzle is | ||
* returned, which is a 4-element <code>char</code> array with all elements | ||
* being 0. <br> | ||
* <br> | ||
* Otherwise, if the given array does not have a length of 4, then an | ||
* <code>IllegalArgumentException</code> is thrown.<br> | ||
* <br> | ||
* If the given swizzle is equal to the default swizzle, then it is returned | ||
* directly. <br> | ||
* <br> | ||
* Otherwise, this swizzle must match the regular expression | ||
* <code>/^[rgba01]{4}$/</code>.<br> | ||
* | ||
* @param inputSwizzle The input swizzle | ||
* @return The validated input swizzle, or a new default swizzle if the input | ||
* was <code>null</code> | ||
* @throws IllegalArgumentException If the given swizzle is not valid | ||
*/ | ||
static char[] validateSwizzle(char inputSwizzle[]) { | ||
char defaultSwizzle[] = new char[4]; | ||
if (inputSwizzle == null) { | ||
return defaultSwizzle; | ||
} | ||
if (inputSwizzle.length != 4) { | ||
throw new IllegalArgumentException("The inputSwizzle must contain 4 characters"); | ||
} | ||
if (Arrays.equals(inputSwizzle, defaultSwizzle)) { | ||
return inputSwizzle; | ||
} | ||
String valid = "rgba01"; | ||
for (int i = 0; i < inputSwizzle.length; i++) { | ||
char c = inputSwizzle[i]; | ||
if (valid.indexOf(c) == -1) { | ||
throw new IllegalArgumentException("The inputSwizzle may only consist of 'rgba01', but contains " + c); | ||
} | ||
} | ||
return inputSwizzle; | ||
|
||
} | ||
|
||
/** | ||
* Private constructor to prevent instantiation | ||
*/ | ||
private KtxUtilities() { | ||
// Private constructor to prevent instantiation | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
interface/java_binding/src/test/java/org/khronos/ktx/KtxUtilitiesTest.java
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,53 @@ | ||
/* | ||
* Copyright (c) 2024, Khronos Group and Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.khronos.ktx; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.io.IOException; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class KtxUtilitiesTest { | ||
@Test | ||
public void testValidSwizzle() { | ||
char swizzle[] = new char[] { 'a', '1', 'r', '0' }; | ||
char expected[] = swizzle; | ||
char actual[] = KtxUtilities.validateSwizzle(swizzle); | ||
assertArrayEquals(expected, actual, "Accepts valid swizzle and returns it"); | ||
} | ||
|
||
@Test | ||
public void testNullSwizzle() { | ||
char expected[] = new char[] { 0, 0, 0, 0 }; | ||
char actual[] = KtxUtilities.validateSwizzle(null); | ||
assertArrayEquals(expected, actual, "Accepts null swizzle (to apply no swizzle), and returns a default"); | ||
} | ||
|
||
@Test | ||
public void testDefaultSwizzle() { | ||
char swizzle[] = new char[] { 0, 0, 0, 0 }; | ||
char expected[] = swizzle; | ||
char actual[] = KtxUtilities.validateSwizzle(swizzle); | ||
assertArrayEquals(expected, actual, "Accepts default swizzle (all zeros)"); | ||
} | ||
|
||
@Test | ||
public void testInvalidSwizzleLength() throws IOException { | ||
char swizzle[] = new char[] { 'a', 'b', 'r', 'g', 'r', 'g' }; | ||
assertThrows(IllegalArgumentException.class, () -> KtxUtilities.validateSwizzle(swizzle), | ||
"Swizzle length != 4 expected to throw IllegalArgumentException"); | ||
} | ||
|
||
@Test | ||
public void testInvalidSwizzleChar() throws IOException { | ||
|
||
char swizzle[] = new char[] { 'a', 'b', 'X', 'g' }; | ||
assertThrows(IllegalArgumentException.class, () -> KtxUtilities.validateSwizzle(swizzle), | ||
"Invalid swizzle character expected to throw IllegalArgumentException"); | ||
} | ||
} |
46 changes: 0 additions & 46 deletions
46
interface/java_binding/src/test/java/org/khronos/ktx/test/KtxAstcParamsTest.java
This file was deleted.
Oops, something went wrong.
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