(module main)

(fn parse_i64 ((text string)) -> (result i64 i32)
  (std.string.parse_i64_result text))

(fn parsed_text ((text string)) -> string
  (std.num.i64_to_string (std.result.unwrap_ok (parse_i64 text))))

(test "parse i64 zero ok"
  (= (parsed_text "0") "0"))

(test "parse i64 negative ok"
  (= (parsed_text "-7") "-7"))

(test "parse i64 low ok"
  (= (parsed_text "-9223372036854775808") "-9223372036854775808"))

(test "parse i64 high ok"
  (= (parsed_text "9223372036854775807") "9223372036854775807"))

(test "parse i64 empty err"
  (let value (result i64 i32) (parse_i64 ""))
  (if (std.result.is_err value)
    (= (std.result.unwrap_err value) 1)
    false))

(test "parse i64 plus err"
  (let value (result i64 i32) (parse_i64 "+1"))
  (if (std.result.is_err value)
    (= (std.result.unwrap_err value) 1)
    false))

(test "parse i64 above range err"
  (let value (result i64 i32) (parse_i64 "9223372036854775808"))
  (if (std.result.is_err value)
    (= (std.result.unwrap_err value) 1)
    false))

(fn main () -> i32
  (let value (result i64 i32) (parse_i64 "-7"))
  (if (std.result.is_ok value)
    (if (= (std.num.i64_to_string (std.result.unwrap_ok value)) "-7")
      0
      1)
    (std.result.unwrap_err value)))
