-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcore_classes.obs
More file actions
105 lines (87 loc) · 2.65 KB
/
core_classes.obs
File metadata and controls
105 lines (87 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#~
# Core Classes Test
# Tests class instantiation, inheritance, method calls, and select statements
# Based on programs/tests/prgm10.obs
~#
class Foo {
@value1 : Int;
@value2 : Float;
New(value1 : Int, value2 : Float) {
@value1 := value1;
@value2 := value2;
}
method : public : GetValue1() ~ Int {
return @value1;
}
method : public : GetValue2() ~ Float {
return @value2;
}
}
class Bar from Foo {
@foos : Foo[];
New(foo: Foo) {
Parent(foo->GetValue1(), foo->GetValue2());
@foos := Foo->New[4];
@foos[0] := Foo->New(1, 1.0);
@foos[1] := Foo->New(2, 2.0);
@foos[2] := Foo->New(3, 3.0);
@foos[3] := Foo->New(4, 4.0);
}
method : public : DoStuff() ~ Float {
a := 13;
return @value1 * a - @value2;
}
method : public : GetFoo() ~ Foo {
return @foos[1];
}
}
class CoreClassesTest {
function : Main(args : String[]) ~ Nil {
"Testing core class operations..."->PrintLine();
pass := true;
# Test basic class instantiation and method calls
foo := Foo->New(7, 3.1415);
if(foo->GetValue1() <> 7) {
pass := false;
" FAIL: foo->GetValue1() = {$foo->GetValue1()}, expected 7"->PrintLine();
};
val2 := foo->GetValue2();
if(val2 < 3.14 | val2 > 3.15) {
pass := false;
" FAIL: foo->GetValue2() = {$val2}, expected ~3.1415"->PrintLine();
};
# Test inheritance
bar := Bar->New(foo);
retrieved_foo := bar->GetFoo();
if(retrieved_foo->GetValue1() <> 2) {
pass := false;
" FAIL: bar->GetFoo()->GetValue1() = {$retrieved_foo->GetValue1()}, expected 2"->PrintLine();
};
# Test select statement with integers
select(10) {
label 7: {
pass := false;
" FAIL: Select matched 7 instead of other"->PrintLine();
}
other: {
# Correct - should reach here
}
};
# Test select with characters
test_char := 'H';
select(test_char) {
label 'H': {
# Correct - should reach here
}
other: {
pass := false;
" FAIL: Select didn't match 'H'"->PrintLine();
}
};
if(pass) {
"PASS: Core class operations"->PrintLine();
} else {
"FAIL: Core class operations"->PrintLine();
};
}
}