Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 170 additions & 0 deletions tests/record/annotations/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
import java.lang.annotation.*;
import java.lang.reflect.*;

// Custom annotations for testing
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.RECORD_COMPONENT, ElementType.PARAMETER})
@interface MyAnnotation {
String value() default "";
int priority() default 1;
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.RECORD_COMPONENT)
@interface NotNull {
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.RECORD_COMPONENT)
@interface Range {
int min() default 0;
int max() default 100;
}

public class Test {

// Annotated record
@MyAnnotation("PersonRecord")
record Person(@NotNull String name, @Range(min = 0, max = 150) int age) {}

// Record with multiple annotations
record Product(
@MyAnnotation("ProductName") @NotNull String name,
@MyAnnotation(value = "ProductPrice", priority = 2) double price,
@Range(min = 0, max = 1000) int quantity
) {}

// Record with annotation on type
@MyAnnotation("ConfigRecord")
record Config(String host, int port, boolean secure) {}

public static void main(String[] args) {
System.out.println("=== Annotations Tests ===");

testRecordAnnotations();
testComponentAnnotations();
testAnnotationReflection();
testMultipleAnnotations();
testAnnotationValues();
}

public static void testRecordAnnotations() {
System.out.println("--- Test Record Annotations ---");
Class<Person> personClass = Person.class;

System.out.println("Person class is annotated: " + personClass.isAnnotationPresent(MyAnnotation.class));

if (personClass.isAnnotationPresent(MyAnnotation.class)) {
MyAnnotation annotation = personClass.getAnnotation(MyAnnotation.class);
System.out.println("Annotation value: " + annotation.value());
System.out.println("Annotation priority: " + annotation.priority());
}

Class<Config> configClass = Config.class;
System.out.println("Config class is annotated: " + configClass.isAnnotationPresent(MyAnnotation.class));

if (configClass.isAnnotationPresent(MyAnnotation.class)) {
MyAnnotation annotation = configClass.getAnnotation(MyAnnotation.class);
System.out.println("Config annotation value: " + annotation.value());
}
}

public static void testComponentAnnotations() {
System.out.println("--- Test Component Annotations ---");
Class<Person> personClass = Person.class;
RecordComponent[] components = personClass.getRecordComponents();

for (RecordComponent component : components) {
System.out.println("Component: " + component.getName());

if (component.isAnnotationPresent(NotNull.class)) {
System.out.println(" Has @NotNull annotation");
}

if (component.isAnnotationPresent(Range.class)) {
Range range = component.getAnnotation(Range.class);
System.out.println(" Has @Range annotation: min=" + range.min() + ", max=" + range.max());
}

Annotation[] annotations = component.getAnnotations();
System.out.println(" Total annotations: " + annotations.length);
}
}

public static void testAnnotationReflection() {
System.out.println("--- Test Annotation Reflection ---");
Class<Product> productClass = Product.class;
RecordComponent[] components = productClass.getRecordComponents();

for (RecordComponent component : components) {
System.out.println("Component: " + component.getName());

Annotation[] annotations = component.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(" Annotation type: " + annotation.annotationType().getSimpleName());

if (annotation instanceof MyAnnotation) {
MyAnnotation myAnnotation = (MyAnnotation) annotation;
System.out.println(" Value: " + myAnnotation.value());
System.out.println(" Priority: " + myAnnotation.priority());
}

if (annotation instanceof Range) {
Range range = (Range) annotation;
System.out.println(" Min: " + range.min());
System.out.println(" Max: " + range.max());
}
}
}
}

public static void testMultipleAnnotations() {
System.out.println("--- Test Multiple Annotations ---");
Product product = new Product("Laptop", 999.99, 50);
Class<Product> productClass = Product.class;

try {
RecordComponent nameComponent = null;
RecordComponent priceComponent = null;

for (RecordComponent component : productClass.getRecordComponents()) {
if ("name".equals(component.getName())) {
nameComponent = component;
} else if ("price".equals(component.getName())) {
priceComponent = component;
}
}

if (nameComponent != null) {
System.out.println("Name component annotations:");
System.out.println(" @MyAnnotation present: " + nameComponent.isAnnotationPresent(MyAnnotation.class));
System.out.println(" @NotNull present: " + nameComponent.isAnnotationPresent(NotNull.class));
}

if (priceComponent != null) {
System.out.println("Price component annotations:");
System.out.println(" @MyAnnotation present: " + priceComponent.isAnnotationPresent(MyAnnotation.class));
if (priceComponent.isAnnotationPresent(MyAnnotation.class)) {
MyAnnotation annotation = priceComponent.getAnnotation(MyAnnotation.class);
System.out.println(" Value: " + annotation.value());
System.out.println(" Priority: " + annotation.priority());
}
}
} catch (Exception e) {
System.out.println("Error accessing annotations: " + e.getMessage());
}
}

public static void testAnnotationValues() {
System.out.println("--- Test Annotation Values ---");
Person person = new Person("Alice", 30);

System.out.println("Created person: " + person);
System.out.println("Person name: " + person.name());
System.out.println("Person age: " + person.age());

// Annotations don't affect runtime behavior directly
// but can be used for validation, documentation, etc.
System.out.println("Annotations are metadata only - person created successfully");
}
}
2 changes: 2 additions & 0 deletions tests/record/annotations/ignore.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
thread '<unknown>' has overflowed its stack
fatal runtime error: stack overflow, aborting
72 changes: 72 additions & 0 deletions tests/record/basic/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
public class Test {

// Basic record definition
record Person(String name, int age) {}

record Point(int x, int y) {}

record Empty() {}

public static void main(String[] args) {
System.out.println("=== Basic Record Tests ===");

testBasicRecordCreation();
testRecordAccessors();
testEmptyRecord();
testRecordWithMultipleComponents();
testRecordInstantiation();
}

public static void testBasicRecordCreation() {
System.out.println("--- Test Basic Record Creation ---");
Person person = new Person("Alice", 30);
System.out.println("Created person: " + person);
System.out.println("Person class: " + person.getClass().getName());
System.out.println("Is record: " + person.getClass().isRecord());
}

public static void testRecordAccessors() {
System.out.println("--- Test Record Accessors ---");
Person person = new Person("Bob", 25);
System.out.println("Name: " + person.name());
System.out.println("Age: " + person.age());

Point point = new Point(10, 20);
System.out.println("X: " + point.x());
System.out.println("Y: " + point.y());
}

public static void testEmptyRecord() {
System.out.println("--- Test Empty Record ---");
Empty empty = new Empty();
System.out.println("Empty record: " + empty);
System.out.println("Empty record class: " + empty.getClass().getName());
System.out.println("Is record: " + empty.getClass().isRecord());
}

public static void testRecordWithMultipleComponents() {
System.out.println("--- Test Record With Multiple Components ---");
record Address(String street, String city, String state, int zipCode) {}

Address address = new Address("123 Main St", "Springfield", "IL", 62701);
System.out.println("Address: " + address);
System.out.println("Street: " + address.street());
System.out.println("City: " + address.city());
System.out.println("State: " + address.state());
System.out.println("Zip: " + address.zipCode());
}

public static void testRecordInstantiation() {
System.out.println("--- Test Record Instantiation ---");
Person[] people = {
new Person("Charlie", 35),
new Person("Diana", 28),
new Person("Eve", 42)
};

for (Person p : people) {
System.out.println("Person: " + p.name() + " is " + p.age() + " years old");
}
}
}

14 changes: 14 additions & 0 deletions tests/record/basic/ignore.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
InternalError("Throwable(Object(Some(Object(RwLock { data: Object(java/lang/NullPointerException)
backtrace=java/lang/StackTraceElement[15]
detailMessage=String(\"array cannot be null\")
cause=Object(class java/lang/NullPointerException)
stackTrace=java/lang/StackTraceElement[0]
depth=int(15)
suppressedExceptions=Object(class java/util/Collections$EmptyList)
extendedMessageState=int(1)
extendedMessage=Object(null)
})))):
stdout: === Basic Record Tests ===
--- Test Basic Record Creation ---

stderr: ")
100 changes: 100 additions & 0 deletions tests/record/constructors/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
public class Test {

// Record with custom constructor
record Person(String name, int age) {
public Person {
if (name == null || name.isEmpty()) {
throw new IllegalArgumentException("Name cannot be null or empty");
}
if (age < 0) {
throw new IllegalArgumentException("Age cannot be negative");
}
}
}

// Record with custom canonical constructor
record Point(int x, int y) {
public Point(int x, int y) {
this.x = Math.max(0, x);
this.y = Math.max(0, y);
}
}

// Record with additional constructors
record Rectangle(int width, int height) {
public Rectangle {
if (width <= 0 || height <= 0) {
throw new IllegalArgumentException("Width and height must be positive");
}
}

// Additional constructor for square
public Rectangle(int size) {
this(size, size);
}
}

public static void main(String[] args) {
System.out.println("=== Constructor Tests ===");

testCompactConstructor();
testCanonicalConstructor();
testAdditionalConstructors();
testConstructorValidation();
}

public static void testCompactConstructor() {
System.out.println("--- Test Compact Constructor ---");
try {
Person person = new Person("Alice", 30);
System.out.println("Valid person created: " + person);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}

public static void testCanonicalConstructor() {
System.out.println("--- Test Canonical Constructor ---");
Point point1 = new Point(-5, 10);
Point point2 = new Point(15, -3);
System.out.println("Point1 (negative x adjusted): " + point1);
System.out.println("Point2 (negative y adjusted): " + point2);
}

public static void testAdditionalConstructors() {
System.out.println("--- Test Additional Constructors ---");
Rectangle rect1 = new Rectangle(10, 20);
Rectangle square = new Rectangle(15);
System.out.println("Rectangle: " + rect1);
System.out.println("Square: " + square);
}

public static void testConstructorValidation() {
System.out.println("--- Test Constructor Validation ---");

// Test invalid name
try {
Person invalidPerson = new Person("", 25);
System.out.println("Should not reach here");
} catch (IllegalArgumentException e) {
System.out.println("Caught expected exception for empty name: " + e.getMessage());
}

// Test invalid age
try {
Person invalidPerson = new Person("Bob", -5);
System.out.println("Should not reach here");
} catch (IllegalArgumentException e) {
System.out.println("Caught expected exception for negative age: " + e.getMessage());
}

// Test invalid rectangle
try {
Rectangle invalidRect = new Rectangle(0, 10);
System.out.println("Should not reach here");
} catch (IllegalArgumentException e) {
System.out.println("Caught expected exception for invalid rectangle: " + e.getMessage());
}
}
}

14 changes: 14 additions & 0 deletions tests/record/constructors/ignore.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
InternalError("Throwable(Object(Some(Object(RwLock { data: Object(java/lang/BootstrapMethodError)
backtrace=java/lang/StackTraceElement[8]
detailMessage=String(\"Invalid bootstrap method descriptor: (Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/TypeDescriptor;Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/invoke/MethodHandle;)Ljava/lang/Object;\")
cause=Object(class java/lang/BootstrapMethodError)
stackTrace=java/lang/StackTraceElement[0]
depth=int(8)
suppressedExceptions=Object(class java/util/Collections$EmptyList)
})))):
stdout: === Constructor Tests ===
--- Test Compact Constructor ---
Error: array cannot be null
--- Test Canonical Constructor ---

stderr: ")
Loading
Loading