2020-03-26 17:03:14 +01:00
|
|
|
struct AA {
|
2019-09-28 13:13:09 +02:00
|
|
|
mut:
|
2020-03-27 08:46:54 +01:00
|
|
|
val int
|
2019-09-28 13:13:09 +02:00
|
|
|
nums []int
|
|
|
|
}
|
2019-07-02 00:00:27 +02:00
|
|
|
|
2020-03-26 17:03:14 +01:00
|
|
|
struct BB {
|
2019-09-28 13:13:09 +02:00
|
|
|
mut:
|
2020-03-26 17:03:14 +01:00
|
|
|
a AA
|
2019-09-28 13:13:09 +02:00
|
|
|
}
|
2019-07-03 23:53:48 +02:00
|
|
|
|
2020-03-26 17:03:14 +01:00
|
|
|
struct CC {
|
2019-09-28 13:13:09 +02:00
|
|
|
mut:
|
2020-03-27 08:46:54 +01:00
|
|
|
b BB
|
2019-09-28 13:13:09 +02:00
|
|
|
nums []int
|
2020-03-26 17:14:24 +01:00
|
|
|
aarr []AA
|
2020-03-27 08:46:54 +01:00
|
|
|
num int
|
2019-09-28 13:13:09 +02:00
|
|
|
}
|
2019-07-03 23:53:48 +02:00
|
|
|
|
|
|
|
struct User {
|
|
|
|
name string
|
2020-03-27 08:46:54 +01:00
|
|
|
age int
|
2019-09-28 13:13:09 +02:00
|
|
|
}
|
2019-07-24 02:22:34 +02:00
|
|
|
|
|
|
|
struct Foo {
|
2019-09-28 13:13:09 +02:00
|
|
|
@type string
|
|
|
|
}
|
|
|
|
|
2020-03-27 08:46:54 +01:00
|
|
|
struct Empty {}
|
2020-03-21 12:24:34 +01:00
|
|
|
|
2020-03-27 08:46:54 +01:00
|
|
|
// We need to make sure that this compiles with all the reserved names.
|
2019-09-28 13:13:09 +02:00
|
|
|
struct ReservedKeywords {
|
|
|
|
delete int
|
|
|
|
exit int
|
|
|
|
unix int
|
|
|
|
error int
|
|
|
|
malloc int
|
2020-03-27 08:46:54 +01:00
|
|
|
calloc int
|
2019-09-28 13:13:09 +02:00
|
|
|
free int
|
|
|
|
panic int
|
|
|
|
auto int
|
|
|
|
char int
|
|
|
|
do int
|
|
|
|
double int
|
|
|
|
extern int
|
|
|
|
float int
|
|
|
|
inline int
|
|
|
|
long int
|
|
|
|
register int
|
|
|
|
restrict int
|
|
|
|
short int
|
|
|
|
signed int
|
|
|
|
typedef int
|
|
|
|
unsigned int
|
|
|
|
void int
|
|
|
|
volatile int
|
|
|
|
while int
|
|
|
|
}
|
|
|
|
|
2020-03-21 12:24:34 +01:00
|
|
|
fn test_empty_struct() {
|
|
|
|
d := &Empty{}
|
|
|
|
println(d) // != voidptr(0)
|
|
|
|
println(sizeof(Empty)) // == 0
|
|
|
|
}
|
|
|
|
|
2019-09-28 13:13:09 +02:00
|
|
|
fn test_struct_levels() {
|
2020-03-26 17:03:14 +01:00
|
|
|
mut c := CC{}
|
2020-03-27 08:46:54 +01:00
|
|
|
println(c.nums.len)
|
2019-09-28 13:13:09 +02:00
|
|
|
assert c.nums.len == 0
|
|
|
|
c.nums << 3
|
|
|
|
assert c.nums.len == 1
|
|
|
|
assert c.nums[0] == 3
|
|
|
|
c.nums[0] = 4
|
|
|
|
assert c.nums[0] == 4
|
|
|
|
c.b.a.val = 34
|
|
|
|
assert c.b.a.val == 34
|
2019-09-15 11:26:05 +02:00
|
|
|
c.b.a.nums = [0].repeat(0)
|
2019-09-28 13:13:09 +02:00
|
|
|
c.b.a.nums << 0
|
|
|
|
c.b.a.nums << 2
|
|
|
|
assert c.b.a.nums.len == 2
|
|
|
|
assert c.b.a.nums[0] == 0
|
|
|
|
assert c.b.a.nums[1] == 2
|
2020-03-27 08:46:54 +01:00
|
|
|
c.b.a.nums[0] = 7
|
2019-09-28 13:13:09 +02:00
|
|
|
assert c.b.a.nums[0] == 7
|
2020-03-27 08:46:54 +01:00
|
|
|
c.aarr << AA{
|
|
|
|
val: 8
|
|
|
|
}
|
2019-09-28 13:13:09 +02:00
|
|
|
assert c.aarr.len == 1
|
|
|
|
assert c.aarr[0].val == 8
|
|
|
|
c.num = 20
|
|
|
|
assert c.num == 20
|
2020-03-27 08:46:54 +01:00
|
|
|
c.aarr[0].val = 10
|
2019-09-28 13:13:09 +02:00
|
|
|
assert c.aarr[0].val == 10
|
|
|
|
}
|
2019-07-03 23:53:48 +02:00
|
|
|
|
|
|
|
fn test_struct_str() {
|
2020-03-27 08:46:54 +01:00
|
|
|
u := User{
|
|
|
|
'Bob',30}
|
|
|
|
println(u) // make sure the struct is printable
|
2019-09-28 13:13:09 +02:00
|
|
|
// assert u.str() == '{name:"Bob", age:30}' // TODO
|
|
|
|
}
|
2019-07-24 02:22:34 +02:00
|
|
|
|
|
|
|
fn test_at() {
|
2020-03-27 08:46:54 +01:00
|
|
|
foo := Foo{
|
|
|
|
@type: 'test'
|
|
|
|
}
|
2019-09-28 13:13:09 +02:00
|
|
|
println(foo.@type)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_reserved_keywords() {
|
2020-03-27 08:46:54 +01:00
|
|
|
// Make sure we can initialize them correctly using full syntax.
|
|
|
|
rk_holder := ReservedKeywords{
|
|
|
|
0,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3}
|
|
|
|
// Test a few as it'll take too long to test all. If it's initialized
|
|
|
|
// correctly, other fields are also probably valid.
|
2019-09-28 13:13:09 +02:00
|
|
|
assert rk_holder.unix == 5
|
|
|
|
assert rk_holder.while == 3
|
2020-03-27 08:46:54 +01:00
|
|
|
rk_holder2 := ReservedKeywords{
|
|
|
|
inline: 9
|
|
|
|
volatile: 11
|
|
|
|
}
|
|
|
|
// Make sure partial initialization works too.
|
2019-09-28 13:13:09 +02:00
|
|
|
assert rk_holder2.inline == 9
|
|
|
|
assert rk_holder2.volatile == 11
|
2020-03-27 08:46:54 +01:00
|
|
|
assert rk_holder2.while == 0 // Zero value as not specified.
|
2019-09-28 13:13:09 +02:00
|
|
|
}
|
2019-10-13 00:50:15 +02:00
|
|
|
|
|
|
|
struct User2 {
|
|
|
|
mut:
|
|
|
|
name string
|
2019-11-26 04:41:56 +01:00
|
|
|
}
|
2019-10-13 00:50:15 +02:00
|
|
|
|
|
|
|
fn test_mutable_fields() {
|
|
|
|
mut u := User2{}
|
|
|
|
u.name = 'Peter'
|
|
|
|
assert u.name == 'Peter'
|
2019-11-26 04:41:56 +01:00
|
|
|
}
|
2019-11-24 13:56:14 +01:00
|
|
|
|
|
|
|
struct Def {
|
|
|
|
a int
|
2020-03-27 08:46:54 +01:00
|
|
|
b int=7
|
2019-11-26 04:41:56 +01:00
|
|
|
}
|
2019-11-24 13:56:14 +01:00
|
|
|
|
|
|
|
fn test_default_vals() {
|
|
|
|
d := Def{}
|
|
|
|
assert d.a == 0
|
|
|
|
assert d.b == 7
|
2020-03-27 08:46:54 +01:00
|
|
|
d2 := Def{
|
|
|
|
10,20}
|
2019-11-24 13:56:14 +01:00
|
|
|
assert d2.a == 10
|
|
|
|
assert d2.b == 20
|
2019-11-26 04:41:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_assoc_with_vars() {
|
2020-03-27 08:46:54 +01:00
|
|
|
def2 := Def{
|
|
|
|
a: 12
|
|
|
|
}
|
|
|
|
merged := {
|
|
|
|
def2 |
|
|
|
|
a:42
|
|
|
|
}
|
2019-11-26 04:41:56 +01:00
|
|
|
assert merged.a == 42
|
|
|
|
assert merged.b == 7
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2020-03-27 08:46:54 +01:00
|
|
|
const_def = Def{
|
|
|
|
a: 100
|
|
|
|
}
|
2019-11-26 04:41:56 +01:00
|
|
|
)
|
2020-03-27 08:46:54 +01:00
|
|
|
|
2019-11-26 04:41:56 +01:00
|
|
|
fn test_assoc_with_constants() {
|
2020-03-27 08:46:54 +01:00
|
|
|
println(1)
|
|
|
|
/*
|
|
|
|
QTODO
|
2019-11-26 04:41:56 +01:00
|
|
|
merged := { const_def | a: 42 }
|
|
|
|
assert merged.a == 42
|
|
|
|
assert merged.b == 7
|
|
|
|
|
|
|
|
again := { const_def | b: 22 }
|
|
|
|
assert again.a == 100
|
|
|
|
assert again.b == 22
|
2020-03-27 08:46:54 +01:00
|
|
|
*/
|
|
|
|
|
2019-11-26 04:41:56 +01:00
|
|
|
}
|
2019-11-24 13:56:14 +01:00
|
|
|
|
2020-03-27 08:46:54 +01:00
|
|
|
struct AttrTest {
|
|
|
|
a int // private immutable (default)
|
2019-12-13 18:09:11 +01:00
|
|
|
mut:
|
2020-03-27 08:46:54 +01:00
|
|
|
b int // private mutable
|
|
|
|
c int // (you can list multiple fields with the same access modifier)
|
2019-12-13 18:09:11 +01:00
|
|
|
pub:
|
2020-03-27 08:46:54 +01:00
|
|
|
d int // public immmutable (readonly)
|
2019-12-13 18:09:11 +01:00
|
|
|
pub mut:
|
2020-03-27 08:46:54 +01:00
|
|
|
e int // public, but mutable only in parent module
|
2019-12-13 18:28:28 +01:00
|
|
|
__global:
|
2020-03-27 08:46:54 +01:00
|
|
|
f int // public and mutable both inside and outside parent module
|
2019-12-13 18:09:11 +01:00
|
|
|
}
|
|
|
|
|
2020-03-27 08:46:54 +01:00
|
|
|
fn fooo() {
|
|
|
|
a := AttrTest{
|
|
|
|
1,2,3,4,5,6}
|
2020-01-21 03:22:18 +01:00
|
|
|
}
|
|
|
|
|
2019-12-12 11:51:05 +01:00
|
|
|
/*
|
|
|
|
[typedef]
|
|
|
|
struct C.fixed {
|
|
|
|
points [10]C.point
|
|
|
|
}
|
|
|
|
|
|
|
|
[typedef]
|
|
|
|
struct C.point {
|
|
|
|
x int
|
|
|
|
y int
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_fixed_field() {
|
|
|
|
f := &C.fixed{}
|
|
|
|
p := f.points[0]
|
|
|
|
//f.nums[0] = 10
|
|
|
|
//println(f.nums[0])
|
|
|
|
println(p.x)
|
|
|
|
//nums: [10]int
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2020-01-24 18:57:32 +01:00
|
|
|
|
|
|
|
struct Config {
|
2020-03-27 08:46:54 +01:00
|
|
|
n int
|
|
|
|
def int=10
|
2020-01-24 18:57:32 +01:00
|
|
|
}
|
|
|
|
|
2020-03-27 08:46:54 +01:00
|
|
|
fn foo_config(c Config) {}
|
2020-01-24 18:57:32 +01:00
|
|
|
|
2020-03-27 08:46:54 +01:00
|
|
|
fn foo2(u User) {}
|
2020-02-18 16:25:09 +01:00
|
|
|
|
2020-01-24 18:57:32 +01:00
|
|
|
fn test_config() {
|
2020-03-27 08:46:54 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
foo_config({
|
|
|
|
n: 10
|
|
|
|
def: 20
|
|
|
|
})
|
2020-01-24 18:57:32 +01:00
|
|
|
foo_config({})
|
2020-03-27 08:46:54 +01:00
|
|
|
foo2({
|
|
|
|
name: 'Peter'
|
|
|
|
})
|
|
|
|
*/
|
2020-01-24 18:57:32 +01:00
|
|
|
}
|