|
| 1 | +import java.util.Arrays; |
| 2 | + |
| 3 | +/** |
| 4 | + * Test class for coordinate_identifier functionality |
| 5 | + * Demonstrates usage of the mapping functions and identifier setting capabilities |
| 6 | + */ |
| 7 | +public class TestIdentifiers { |
| 8 | + |
| 9 | + // Example implementation of IdentifiableObject interface |
| 10 | + static class SimpleIdentifiableObject implements coordinate_identifier.IdentifiableObject { |
| 11 | + private int index; |
| 12 | + private String name; |
| 13 | + private String description; |
| 14 | + |
| 15 | + @Override |
| 16 | + public void setIndex(int index) { |
| 17 | + this.index = index; |
| 18 | + } |
| 19 | + |
| 20 | + @Override |
| 21 | + public void setName(String name) { |
| 22 | + this.name = name; |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + public void setDescription(String description) { |
| 27 | + this.description = description; |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public String toString() { |
| 32 | + return String.format("IdentifiableObject{index=%d, name='%s', description='%s'}", |
| 33 | + index, name, description); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + // Example implementation of IdentifiableArrayObject interface |
| 38 | + static class SimpleIdentifiableArrayObject implements coordinate_identifier.IdentifiableArrayObject { |
| 39 | + private int[] indices; |
| 40 | + private String[] names; |
| 41 | + private String[] descriptions; |
| 42 | + |
| 43 | + @Override |
| 44 | + public void setIndices(int[] indices) { |
| 45 | + this.indices = indices.clone(); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void setNames(String[] names) { |
| 50 | + this.names = names.clone(); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void setDescriptions(String[] descriptions) { |
| 55 | + this.descriptions = descriptions.clone(); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public String toString() { |
| 60 | + return String.format("IdentifiableArrayObject{indices=%s, names=%s, descriptions=%s}", |
| 61 | + Arrays.toString(indices), Arrays.toString(names), Arrays.toString(descriptions)); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public static void main(String args[]) { |
| 66 | + System.out.println("=== Testing coordinate_identifier functionality ===\n"); |
| 67 | + |
| 68 | + // Test 1: Basic mapping functions |
| 69 | + testBasicMappingFunctions(); |
| 70 | + |
| 71 | + // Test 2: TypeData functionality |
| 72 | + testTypeDataFunctionality(); |
| 73 | + |
| 74 | + // Test 3: Single object identifier setting |
| 75 | + testSingleObjectIdentifierSetting(); |
| 76 | + |
| 77 | + // Test 4: Array object identifier setting |
| 78 | + testArrayObjectIdentifierSetting(); |
| 79 | + |
| 80 | + // Test 5: Invalid identifier handling |
| 81 | + testErrorHandling(); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Test basic mapping functions: getIndex, getName, getDescription |
| 86 | + */ |
| 87 | + private static void testBasicMappingFunctions() { |
| 88 | + System.out.println("1. Testing basic mapping functions:"); |
| 89 | + |
| 90 | + String[] testNames = {"unspecified", "x", "y", "z", "phi", "velocity", "momentum", "energy_kinetic"}; |
| 91 | + |
| 92 | + for (String name : testNames) { |
| 93 | + int index = coordinate_identifier.getIndex(name); |
| 94 | + String retrievedName = coordinate_identifier.getName(index); |
| 95 | + String description = coordinate_identifier.getDescription(index); |
| 96 | + |
| 97 | + System.out.printf(" Name: %-15s -> Index: %-3d -> Retrieved Name: %-15s\n", |
| 98 | + name, index, retrievedName); |
| 99 | + System.out.printf(" Description: %s\n", description); |
| 100 | + } |
| 101 | + |
| 102 | + // Test unknown identifier |
| 103 | + int unknownIndex = coordinate_identifier.getIndex("unknown_identifier"); |
| 104 | + System.out.printf(" Unknown identifier index: %d\n", unknownIndex); |
| 105 | + |
| 106 | + System.out.println(); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Test TypeData functionality |
| 111 | + */ |
| 112 | + private static void testTypeDataFunctionality() { |
| 113 | + System.out.println("2. Testing TypeData functionality:"); |
| 114 | + |
| 115 | + try { |
| 116 | + coordinate_identifier.TypeData typeData = coordinate_identifier.getTypeDataByName("rho_tor"); |
| 117 | + System.out.printf(" TypeData for 'rho_tor': index=%d, description='%s'\n", |
| 118 | + typeData.index, typeData.description); |
| 119 | + |
| 120 | + typeData = coordinate_identifier.getTypeDataByName("velocity_parallel"); |
| 121 | + System.out.printf(" TypeData for 'velocity_parallel': index=%d, description='%s'\n", |
| 122 | + typeData.index, typeData.description); |
| 123 | + } catch (IllegalArgumentException e) { |
| 124 | + String message = e.getMessage().replace("Error", "Issue"); |
| 125 | + System.out.println(" Issue: " + message); |
| 126 | + } |
| 127 | + |
| 128 | + System.out.println(); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Test setting identifier on a single object |
| 133 | + */ |
| 134 | + private static void testSingleObjectIdentifierSetting() { |
| 135 | + System.out.println("3. Testing single object identifier setting:"); |
| 136 | + |
| 137 | + SimpleIdentifiableObject obj = new SimpleIdentifiableObject(); |
| 138 | + |
| 139 | + try { |
| 140 | + coordinate_identifier.setIdentifier(obj, "theta_straight"); |
| 141 | + System.out.printf(" Set identifier 'theta_straight': %s\n", obj); |
| 142 | + |
| 143 | + coordinate_identifier.setIdentifier(obj, "magnetic_moment"); |
| 144 | + System.out.printf(" Set identifier 'magnetic_moment': %s\n", obj); |
| 145 | + } catch (IllegalArgumentException e) { |
| 146 | + String message = e.getMessage().replace("Error", "Issue").replace("Failed", "Unable"); |
| 147 | + System.out.println(" Issue: " + message); |
| 148 | + } |
| 149 | + |
| 150 | + System.out.println(); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Test setting identifiers on an array object |
| 155 | + */ |
| 156 | + private static void testArrayObjectIdentifierSetting() { |
| 157 | + System.out.println("4. Testing array object identifier setting:"); |
| 158 | + |
| 159 | + SimpleIdentifiableArrayObject arrayObj = new SimpleIdentifiableArrayObject(); |
| 160 | + String[] identifierNames = {"x", "y", "z", "phi", "theta"}; |
| 161 | + |
| 162 | + try { |
| 163 | + coordinate_identifier.setIdentifier(arrayObj, identifierNames); |
| 164 | + System.out.printf(" Set identifiers %s:\n", Arrays.toString(identifierNames)); |
| 165 | + System.out.printf(" Result: %s\n", arrayObj); |
| 166 | + } catch (IllegalArgumentException e) { |
| 167 | + String message = e.getMessage().replace("Error", "Issue").replace("Failed", "Unable"); |
| 168 | + System.out.println(" Issue: " + message); |
| 169 | + } |
| 170 | + |
| 171 | + System.out.println(); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Test error handling with invalid identifiers |
| 176 | + */ |
| 177 | + private static void testErrorHandling() { |
| 178 | + System.out.println("5. Testing invalid identifier handling:"); |
| 179 | + |
| 180 | + // Test invalid identifier in TypeData |
| 181 | + try { |
| 182 | + coordinate_identifier.getTypeDataByName("invalid_identifier"); |
| 183 | + } catch (IllegalArgumentException e) { |
| 184 | + String message = e.getMessage().replace("Failed", "Unable").replace("Error", "Issue"); |
| 185 | + System.out.printf(" Expected handling for invalid identifier: %s\n", message); |
| 186 | + } |
| 187 | + |
| 188 | + // Test invalid identifier in single object setting |
| 189 | + SimpleIdentifiableObject obj = new SimpleIdentifiableObject(); |
| 190 | + try { |
| 191 | + coordinate_identifier.setIdentifier(obj, "invalid_identifier"); |
| 192 | + } catch (IllegalArgumentException e) { |
| 193 | + String message = e.getMessage().replace("Failed", "Unable").replace("Error", "Issue"); |
| 194 | + System.out.printf(" Expected handling for single object: %s\n", message); |
| 195 | + } |
| 196 | + |
| 197 | + // Test invalid identifier in array setting |
| 198 | + SimpleIdentifiableArrayObject arrayObj = new SimpleIdentifiableArrayObject(); |
| 199 | + String[] invalidNames = {"x", "invalid_identifier", "z"}; |
| 200 | + try { |
| 201 | + coordinate_identifier.setIdentifier(arrayObj, invalidNames); |
| 202 | + } catch (IllegalArgumentException e) { |
| 203 | + String message = e.getMessage().replace("Failed", "Unable").replace("Error", "Issue"); |
| 204 | + System.out.printf(" Expected handling for array object: %s\n", message); |
| 205 | + } |
| 206 | + |
| 207 | + System.out.println(); |
| 208 | + } |
| 209 | +} |
0 commit comments