Newer
Older
CodeGenStructString = "lib LibFoo; struct Bar; x : Int32; y : Float32; end; end"
describe "Code gen: struct" do
it "codegens struct property default value" do
run("#{CodeGenStructString}; bar = Pointer(LibFoo::Bar).malloc(1_u64); bar.value.x").to_i.should eq(0)
end
it "codegens struct property setter" do
run("#{CodeGenStructString}; bar = LibFoo::Bar.new; bar.y = 2.5_f32; bar.y").to_f32.should eq(2.5)
it "codegens struct property setter via pointer" do
run("#{CodeGenStructString}; bar = Pointer(LibFoo::Bar).malloc(1_u64); bar.value.y = 2.5_f32; bar.value.y").to_f32.should eq(2.5)
Ary Borenszweig
committed
it "codegens struct property setter via pointer" do
run("#{CodeGenStructString}; bar = Pointer(LibFoo::Bar).malloc(1_u64); bar.value.y = 2.5_f32; bar.value.y").to_f32.should eq(2.5)
end
it "codegens set struct value with constant" do
run("#{CodeGenStructString}; CONST = 1; bar = LibFoo::Bar.new; bar.x = CONST; bar.x").to_i.should eq(1)
end
it "codegens union inside struct" do
run("
union Bar
x : Int32
y : Int64
end
struct Baz
lala : Bar
end
end
a.value.lala.x = 10
a.value.lala.x
").to_i.should eq(10)
end
it "codegens struct get inside struct" do
run("
struct Bar
y : Int32
end
struct Foo
x : Int32
bar : Bar
end
end
((foo as Int32*) + 1_i64).value = 2
foo.value.bar.y
").to_i.should eq(2)
end
it "codegens struct set inside struct" do
run("
struct Bar
y : Int32
end
struct Foo
x : Int32
bar : Bar
end
end
foo = Pointer(LibC::Foo).malloc(1_u64)
bar = LibC::Bar.new
Ary Borenszweig
committed
bar.y = 2
foo.value.bar = bar
foo.value.bar.y
").to_i.should eq(2)
end
it "codegens pointer malloc of struct" do
run("
struct Foo
x : Int32
end
end
p.value.x = 1
p.value.x
").to_i.should eq(1)
end
it "passes struct to method (1)" do
run("
struct Foo
x : Int32
end
end
def foo(f)
f.x = 2
f
end
f1.x = 1
f2 = foo(f1)
f1.x
").to_i.should eq(1)
end
it "passes struct to method (2)" do
run("
struct Foo
x : Int32
end
end
def foo(f)
f.x = 2
f
end
f1.x = 1
f2 = foo(f1)
f2.x
").to_i.should eq(2)
end
it "codegens struct access with -> and then ." do
run("
struct ScalarEvent
x : Int32
end
union EventData
scalar : ScalarEvent
end
struct Event
data : EventData
end
end
e.value.data.scalar.x
").to_i.should eq(0)
end
it "yields struct via ->" do
run("
struct ScalarEvent
x : Int32
end
union EventData
scalar : ScalarEvent
end
struct Event
data : EventData
end
end
def foo
yield e.value.data
end
foo do |data|
data.scalar.x
end
").to_i.should eq(0)
end
it "codegens assign struct to union" do
run("
struct Coco
x : Int32
end
end
it "codegens passing pointerof(struct) to fun" do
run("
struct Foo
a : Int32
end
end
x.value.a
f.a = 1
foo pointerof(f)
").to_i.should eq(1)
end
Ary Borenszweig
committed
Ary Borenszweig
committed
it "builds struct setter with fun type (1)" do
Ary Borenszweig
committed
build(%(
require "prelude"
Ary Borenszweig
committed
struct Foo
x : ->
end
end
Ary Borenszweig
committed
foo.x = -> { }
))
end
Ary Borenszweig
committed
it "builds struct setter with fun type (2)" do
Ary Borenszweig
committed
build(%(
require "prelude"
Ary Borenszweig
committed
struct Foo
x : ->
end
end
foo.value.x = -> { }
Ary Borenszweig
committed
))
end
struct A; end
struct B; end
struct A
x : B*
y : Int32
end
struct B
x : A*
y : Int32
end
end
b.value.y = 2
a.x = b
a.y + a.x.value.y
)).to_i.should eq(3)
end
it "allows using named arguments for new" do
run(%(
struct Point
x, y : Int32
end
end
point.x + point.y
)).to_i.should eq(3)
end
it "returns big struct" do
build(%(
struct Big
x : Int64
y : Int64
z : Int32
end
fun foo(y : Int32) : Big
end
Ary Borenszweig
committed
it "does to_s" do
run(%(
require "prelude"
lib LibFoo
struct Point
x, y : Int32
end
end
point = LibFoo::Point.new x: 1, y: 2
point.to_s
)).to_string.should eq("LibFoo::Point(@x=1, @y=2)")
end