greater_eq.carbon 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. //
  5. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/primitives.carbon
  6. //
  7. // AUTOUPDATE
  8. // TIP: To test this file alone, run:
  9. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/builtins/int/greater_eq.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/builtins/int/greater_eq.carbon
  12. // --- int_greater_eq.carbon
  13. library "[[@TEST_NAME]]";
  14. fn GreaterEq(a: i32, b: i32) -> bool = "int.greater_eq";
  15. fn Negate(a: i32) -> i32 = "int.snegate";
  16. class True {}
  17. class False {}
  18. fn F(true_: True, false_: False) {
  19. false_ as (if GreaterEq(1, 2) then True else False);
  20. true_ as (if GreaterEq(1, 1) then True else False);
  21. true_ as (if GreaterEq(1, 0) then True else False);
  22. false_ as (if GreaterEq(Negate(1), 0) then True else False);
  23. true_ as (if GreaterEq(0, Negate(1)) then True else False);
  24. }
  25. fn RuntimeCallIsValid(a: i32, b: i32) -> bool {
  26. //@dump-sem-ir-begin
  27. return GreaterEq(a, b);
  28. //@dump-sem-ir-end
  29. }
  30. // --- literal.carbon
  31. library "[[@TEST_NAME]]";
  32. fn GreaterEq(a: Core.IntLiteral(), b: Core.IntLiteral()) -> bool = "int.greater_eq";
  33. class Expect(B:! bool) {}
  34. fn Test(B:! bool) -> Expect(B) { return {}; }
  35. fn F() {
  36. Test(GreaterEq(5, 5)) as Expect(true);
  37. Test(GreaterEq(5, 6)) as Expect(false);
  38. Test(GreaterEq(6, 5)) as Expect(true);
  39. Test(GreaterEq(-1, -1)) as Expect(true);
  40. Test(GreaterEq(-1, 1)) as Expect(false);
  41. Test(GreaterEq(1, -1)) as Expect(true);
  42. }
  43. // --- mixed.carbon
  44. library "[[@TEST_NAME]]";
  45. fn GreaterEq(a: Core.IntLiteral(), b: i32) -> bool = "int.greater_eq";
  46. class Expect(B:! bool) {}
  47. fn Test(B:! bool) -> Expect(B) { return {}; }
  48. fn F() {
  49. Test(GreaterEq(5, 5)) as Expect(true);
  50. Test(GreaterEq(5, 6)) as Expect(false);
  51. Test(GreaterEq(6, 5)) as Expect(true);
  52. Test(GreaterEq(-1, -1)) as Expect(true);
  53. Test(GreaterEq(-1, 1)) as Expect(false);
  54. Test(GreaterEq(1, -1)) as Expect(true);
  55. }
  56. // CHECK:STDOUT: --- int_greater_eq.carbon
  57. // CHECK:STDOUT:
  58. // CHECK:STDOUT: constants {
  59. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  60. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  61. // CHECK:STDOUT: %GreaterEq.type: type = fn_type @GreaterEq [concrete]
  62. // CHECK:STDOUT: %GreaterEq: %GreaterEq.type = struct_value () [concrete]
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT:
  65. // CHECK:STDOUT: imports {
  66. // CHECK:STDOUT: }
  67. // CHECK:STDOUT:
  68. // CHECK:STDOUT: fn @RuntimeCallIsValid(%a.param: %i32, %b.param: %i32) -> bool {
  69. // CHECK:STDOUT: !entry:
  70. // CHECK:STDOUT: %GreaterEq.ref: %GreaterEq.type = name_ref GreaterEq, file.%GreaterEq.decl [concrete = constants.%GreaterEq]
  71. // CHECK:STDOUT: %a.ref: %i32 = name_ref a, %a
  72. // CHECK:STDOUT: %b.ref: %i32 = name_ref b, %b
  73. // CHECK:STDOUT: %GreaterEq.call: init bool = call %GreaterEq.ref(%a.ref, %b.ref)
  74. // CHECK:STDOUT: return %GreaterEq.call to %return
  75. // CHECK:STDOUT: }
  76. // CHECK:STDOUT: