From 5941a46a074fd079c4e0833ca0b65ede1903c053 Mon Sep 17 00:00:00 2001
From: Ary Borenszweig <aborenszweig@manas.com.ar>
Date: Sun, 25 Jan 2015 16:24:27 -0300
Subject: [PATCH] Fixed sizeof specs for 32 bits.

---
 spec/compiler/codegen/sizeof_spec.cr | 35 ++++++++++++++++------------
 1 file changed, 20 insertions(+), 15 deletions(-)

diff --git a/spec/compiler/codegen/sizeof_spec.cr b/spec/compiler/codegen/sizeof_spec.cr
index 13c92f4401..a208ba5eeb 100644
--- a/spec/compiler/codegen/sizeof_spec.cr
+++ b/spec/compiler/codegen/sizeof_spec.cr
@@ -19,6 +19,7 @@ describe "Code gen: sizeof" do
   end
 
   it "gets sizeof class" do
+    # A class is represented as a pointer to its data
     run("
       class Foo
         def initialize(@x, @y, @z)
@@ -28,26 +29,30 @@ describe "Code gen: sizeof" do
       Foo.new(1, 2, 3)
 
       sizeof(Foo)
-      ").to_i.should eq(8)
+      ").to_i.should eq(sizeof(Void*))
   end
 
   it "gets sizeof union" do
-    run("
+    size = run("
       sizeof(Int32 | Float64)
-      ").to_i.should eq(16)
-  end
+      ").to_i
 
-  it "gets sizeof class" do
-    run("
-      class Foo
-        def initialize(@x, @y, @z)
-        end
-      end
-
-      Foo.new(1, 2, 3)
-
-      sizeof(Foo)
-      ").to_i.should eq(8)
+    # This union is represented as:
+    #
+    #   struct {
+    #      4 bytes, # for the type id
+    #      8 bytes, # for the largest size between Int32 and Float64
+    #   }
+    #
+    # But in 64 bits structs are aligned to 8 bytes, so it'll actually
+    # be struct { 8 bytes, 8 bytes }.
+    #
+    # In 32 bits structs are aligned to 4 bytes, so it remains the same.
+    ifdef x86_64
+      size.should eq(16)
+    else
+      size.should eq(12)
+    end
   end
 
   it "gets instance_sizeof class" do
-- 
GitLab