// test_mircall_phase2_constructor.hako — MirCall Phase 2 tests (Constructor)
// Expected: Constructor calls (new ArrayBox(), new MapBox(), etc.) work correctly

using selfhost.vm.hakorune-vm.hakorune_vm_core as HakoruneVmCore
using selfhost.shared.common.string_helpers as StringHelpers

static box Main {
  main() {
    print("=== MirCall Phase 2 Tests (Constructor) ===")

    // Test 1: new ArrayBox() - birth()なしConstructor
    // Block 0:
    //   mir_call new ArrayBox() → v1
    //   const 42 → v2
    //   mir_call v1.push(v2) → null
    //   mir_call v1.length() → v3
    //   ret v3
    local mir1 = r#"{"functions":[{"name":"test","blocks":[{"id":0,"instructions":[{"op":"mir_call","dst":1,"mir_call":{"callee":{"type":"Constructor","box_type":"ArrayBox"},"args":[],"effects":["alloc"],"flags":{}}},{"op":"const","dst":2,"value":{"type":"i64","value":42}},{"op":"mir_call","dst":null,"mir_call":{"callee":{"type":"Method","receiver":1,"method":"push"},"args":[2],"effects":[],"flags":{}}},{"op":"mir_call","dst":3,"mir_call":{"callee":{"type":"Method","receiver":1,"method":"size"},"args":[],"effects":[],"flags":{}}},{"op":"ret","value":3}],"terminator":{"op":"ret","value":3}}]}]}"#

    print("[Test 1] new ArrayBox() + push + size (expect 1)")
    local result1 = HakoruneVmCore.run(mir1)
    print("Test 1 result: " + StringHelpers.int_to_str(result1))
    if result1 != 1 {
      print("[FAIL] Test 1: expected 1 (array size), got " + StringHelpers.int_to_str(result1))
      return 1
    }
    print("[PASS] Test 1: new ArrayBox() works")

    // Test 2: new MapBox() - birth()なしConstructor
    // Block 0:
    //   mir_call new MapBox() → v1
    //   mir_call v1.length() → v2
    //   ret v2
    local mir2 = r#"{"functions":[{"name":"test","blocks":[{"id":0,"instructions":[{"op":"mir_call","dst":1,"mir_call":{"callee":{"type":"Constructor","box_type":"MapBox"},"args":[],"effects":["alloc"],"flags":{}}},{"op":"mir_call","dst":2,"mir_call":{"callee":{"type":"Method","receiver":1,"method":"size"},"args":[],"effects":[],"flags":{}}},{"op":"ret","value":2}],"terminator":{"op":"ret","value":2}}]}]}"#

    print("[Test 2] new MapBox() + size (expect 0, empty)")
    local result2 = HakoruneVmCore.run(mir2)
    print("Test 2 result: " + StringHelpers.int_to_str(result2))
    if result2 != 0 {
      print("[FAIL] Test 2: expected 0 (empty map size), got " + StringHelpers.int_to_str(result2))
      return 1
    }
    print("[PASS] Test 2: new MapBox() works")

    // Test 3: Multiple Constructors in single block
    // Block 0:
    //   mir_call new ArrayBox() → v1
    //   const 10 → v2
    //   mir_call v1.push(v2) → null
    //   mir_call new ArrayBox() → v3
    //   const 20 → v4
    //   mir_call v3.push(v4) → null
    //   mir_call v1.length() → v5
    //   mir_call v3.length() → v6
    //   binop Add v5+v6 → v7
    //   ret v7
    local mir3 = r#"{"functions":[{"name":"test","blocks":[{"id":0,"instructions":[{"op":"mir_call","dst":1,"mir_call":{"callee":{"type":"Constructor","box_type":"ArrayBox"},"args":[],"effects":["alloc"],"flags":{}}},{"op":"const","dst":2,"value":{"type":"i64","value":10}},{"op":"mir_call","dst":null,"mir_call":{"callee":{"type":"Method","receiver":1,"method":"push"},"args":[2],"effects":[],"flags":{}}},{"op":"mir_call","dst":3,"mir_call":{"callee":{"type":"Constructor","box_type":"ArrayBox"},"args":[],"effects":["alloc"],"flags":{}}},{"op":"const","dst":4,"value":{"type":"i64","value":20}},{"op":"mir_call","dst":null,"mir_call":{"callee":{"type":"Method","receiver":3,"method":"push"},"args":[4],"effects":[],"flags":{}}},{"op":"mir_call","dst":5,"mir_call":{"callee":{"type":"Method","receiver":1,"method":"size"},"args":[],"effects":[],"flags":{}}},{"op":"mir_call","dst":6,"mir_call":{"callee":{"type":"Method","receiver":3,"method":"size"},"args":[],"effects":[],"flags":{}}},{"op":"binop","op_kind":"Add","dst":7,"lhs":5,"rhs":6},{"op":"ret","value":7}],"terminator":{"op":"ret","value":7}}]}]}"#

    print("[Test 3] Multiple Constructors (expect 2, 1+1)")
    local result3 = HakoruneVmCore.run(mir3)
    print("Test 3 result: " + StringHelpers.int_to_str(result3))
    if result3 != 2 {
      print("[FAIL] Test 3: expected 2 (1+1), got " + StringHelpers.int_to_str(result3))
      return 1
    }
    print("[PASS] Test 3: Multiple Constructors work")

    print("=== All MirCall Phase 2 (Constructor) tests PASSED! (3/3) ===")
    return 0
  }
}
