Files
Viper_RegressionTesting/ClassArrayTest/App/class_test.vp
T
2026-10-17 21:57:03 +08:00

212 lines
5.5 KiB
Plaintext

import stdio
import stdlib
struct SimpleData:
x: int
y: int
struct Point3D:
x: float
y: float
z: float
class Student:
name: char*
age: int
score: int
def __init__(self, n: char*, a: int, s: int):
self.name = n
self.age = a
self.score = s
def get_age(self) -> int:
return self.age
def get_score(self) -> int:
return self.score
class Counter:
value: int
def __init__(self, initial: int):
self.value = initial
def increment(self):
self.value += 1
def get_value(self) -> int:
return self.value
struct StackObj:
data: int
next_ptr: rawptr
def TestBasicDataClass():
stdio.printf("=== Test Basic Data Class ===\n")
p: SimpleData = SimpleData()
p.x = 10
p.y = 20
stdio.printf("SimpleData: x=%d, y=%d\n", p.x, p.y)
def TestPoint3DClass():
stdio.printf("=== Test Point3D Class ===\n")
pt: Point3D = Point3D()
pt.x = 1.5
pt.y = 2.5
pt.z = 3.5
stdio.printf("Point3D: x=%.1f, y=%.1f, z=%.1f\n", pt.x, pt.y, pt.z)
def TestStudentClass():
stdio.printf("=== Test Student Class ===\n")
s: Student = Student("Alice", 20, 95)
stdio.printf("Student: name=%s, age=%d, score=%d\n", s.name, s.age, s.score)
stdio.printf(" get_age()=%d, get_score()=%d\n", s.get_age(), s.get_score())
def TestCounterClass():
stdio.printf("=== Test Counter Class ===\n")
cnt: Counter = Counter(0)
stdio.printf("Initial value: %d\n", cnt.get_value())
cnt.increment()
cnt.increment()
cnt.increment()
stdio.printf("After 3 increments: %d\n", cnt.get_value())
def TestStackAllocation():
stdio.printf("=== Test Stack Allocation ===\n")
obj1: SimpleData = SimpleData()
obj1.x = 100
obj1.y = 200
stdio.printf("Stack obj1: x=%d, y=%d\n", obj1.x, obj1.y)
obj2: Point3D = Point3D()
obj2.x = 10.0
obj2.y = 20.0
obj2.z = 30.0
stdio.printf("Stack obj2: x=%.1f, y=%.1f, z=%.1f\n", obj2.x, obj2.y, obj2.z)
def TestHeapAllocation():
stdio.printf("=== Test Heap Allocation ===\n")
ptr: rawptr = stdlib.malloc(1024)
if ptr != 0:
stdio.printf("Allocated 1024 bytes at %p\n", ptr)
stdlib.free(ptr)
stdio.printf("Freed memory\n")
else:
stdio.printf("malloc failed\n")
def TestHeapObjectWithInit():
stdio.printf("=== Test Heap Object with Init ===\n")
mem_size: sizet = 256
ptr: rawptr = stdlib.malloc(mem_size)
if ptr != 0:
stdio.printf("Allocated %d bytes\n", mem_size)
stdlib.free(ptr)
else:
stdio.printf("malloc failed\n")
def TestMultipleStackObjects():
stdio.printf("=== Test Multiple Stack Objects ===\n")
objs: SimpleData = SimpleData()
objs.x = 1
objs.y = 2
obj2: Point3D = Point3D()
obj2.x = 5.0
obj2.y = 10.0
obj2.z = 15.0
obj3: Counter = Counter(42)
stdio.printf("objs: x=%d, y=%d\n", objs.x, objs.y)
stdio.printf("obj2: x=%.1f, y=%.1f, z=%.1f\n", obj2.x, obj2.y, obj2.z)
stdio.printf("obj3 initial: %d\n", obj3.get_value())
obj3.increment()
obj3.increment()
stdio.printf("obj3 after 2 increments: %d\n", obj3.get_value())
def TestImplicitInitCall():
stdio.printf("=== Test Implicit Init Call ===\n")
stu1: Student = Student("Bob", 22, 88)
stu2: Student = Student("Charlie", 19, 77)
stdio.printf("stu1: name=%s, age=%d, score=%d\n", stu1.name, stu1.age, stu1.score)
stdio.printf("stu2: name=%s, age=%d, score=%d\n", stu2.name, stu2.age, stu2.score)
def TestClassWithBitFields():
stdio.printf("=== Test Class with Bit Fields ===\n")
flags: StackObj = StackObj()
flags.data = 0
flags.next_ptr = 0
stdio.printf("StackObj: data=%d, next_ptr=%p\n", flags.data, flags.next_ptr)
def TestIntArray():
stdio.printf("=== Test Int Array ===\n")
arr: [5]int = None
for i in range(5):
arr[i] = i * 10
for i in range(5):
stdio.printf("arr[%d]=%d\n", i, arr[i])
def TestFloatArray():
stdio.printf("=== Test Float Array ===\n")
farr: [4]float = [1.5, 2.5, 3.5, 4.5]
for i in range(4):
stdio.printf("farr[%d]=%.1f\n", i, farr[i])
def TestCharArray():
stdio.printf("=== Test Char Array (String) ===\n")
str_arr: [32]char = ['H', 'e', 'l', 'l', 'o', '\0']
stdio.printf("String: %s\n", str_arr)
def TestNestedArray():
stdio.printf("=== Test Nested Array (Array of Arrays) ===\n")
row0: [3]int = [0, 1, 2]
row1: [3]int = [10, 11, 12]
for i in range(3):
stdio.printf("row0[%d]=%d ", i, row0[i])
stdio.printf("\n")
for i in range(3):
stdio.printf("row1[%d]=%d ", i, row1[i])
stdio.printf("\n")
def TestArrayWithStruct():
stdio.printf("=== Test Array with Struct ===\n")
points: [3]SimpleData = None
for i in range(3):
points[i].x = i * 10
points[i].y = i * 100
for i in range(3):
stdio.printf("Point[%d]: x=%d, y=%d\n", i, points[i].x, points[i].y)
def TestArrayPointer():
stdio.printf("=== Test Array Pointer ===\n")
data: [8]int = [100, 200, 300]
ptr: int* = data
stdio.printf("ptr[0]=%d\n", ptr[0])
stdio.printf("ptr[1]=%d\n", ptr[1])
stdio.printf("ptr[2]=%d\n", ptr[2])
def TestDynamicString():
stdio.printf("=== Test Dynamic String ===\n")
name: str = "ViperC"
stdio.printf("Hello %s!\n", name)