From 8fddc944ecff4b1c676a96e6966254b0cd6ddb0a Mon Sep 17 00:00:00 2001 From: Filip Sajdak Date: Sat, 22 Apr 2023 21:49:45 +0200 Subject: [PATCH] Add tests --- .../pure2-nested-ifs-conditions-error.cpp2 | 165 ++++++++++++++++ regression-tests/pure2-nested-ifs-error.cpp2 | 27 +++ regression-tests/pure2-nested-ifs.cpp2 | 52 +++++ .../pure2-nested-ifs.cpp.execution | 1 + .../pure2-nested-ifs.cpp.output | 0 .../clang-12/pure2-nested-ifs.cpp.execution | 1 + .../clang-12/pure2-nested-ifs.cpp.output | 0 .../gcc-10/pure2-nested-ifs.cpp.execution | 1 + .../gcc-10/pure2-nested-ifs.cpp.output | 0 .../msvc-2022/pure2-nested-ifs.cpp.execution | 1 + .../msvc-2022/pure2-nested-ifs.cpp.output | 0 .../pure2-nested-ifs-conditions-error.cpp | 187 ++++++++++++++++++ ...e2-nested-ifs-conditions-error.cpp2.output | 2 + .../pure2-nested-ifs-error.cpp2.output | 9 + .../test-results/pure2-nested-ifs.cpp | 74 +++++++ .../test-results/pure2-nested-ifs.cpp2.output | 2 + 16 files changed, 522 insertions(+) create mode 100644 regression-tests/pure2-nested-ifs-conditions-error.cpp2 create mode 100644 regression-tests/pure2-nested-ifs-error.cpp2 create mode 100644 regression-tests/pure2-nested-ifs.cpp2 create mode 100644 regression-tests/test-results/apple-clang-14/pure2-nested-ifs.cpp.execution create mode 100644 regression-tests/test-results/apple-clang-14/pure2-nested-ifs.cpp.output create mode 100644 regression-tests/test-results/clang-12/pure2-nested-ifs.cpp.execution create mode 100644 regression-tests/test-results/clang-12/pure2-nested-ifs.cpp.output create mode 100644 regression-tests/test-results/gcc-10/pure2-nested-ifs.cpp.execution create mode 100644 regression-tests/test-results/gcc-10/pure2-nested-ifs.cpp.output create mode 100644 regression-tests/test-results/msvc-2022/pure2-nested-ifs.cpp.execution create mode 100644 regression-tests/test-results/msvc-2022/pure2-nested-ifs.cpp.output create mode 100644 regression-tests/test-results/pure2-nested-ifs-conditions-error.cpp create mode 100644 regression-tests/test-results/pure2-nested-ifs-conditions-error.cpp2.output create mode 100644 regression-tests/test-results/pure2-nested-ifs-error.cpp2.output create mode 100644 regression-tests/test-results/pure2-nested-ifs.cpp create mode 100644 regression-tests/test-results/pure2-nested-ifs.cpp2.output diff --git a/regression-tests/pure2-nested-ifs-conditions-error.cpp2 b/regression-tests/pure2-nested-ifs-conditions-error.cpp2 new file mode 100644 index 000000000..665aa3181 --- /dev/null +++ b/regression-tests/pure2-nested-ifs-conditions-error.cpp2 @@ -0,0 +1,165 @@ +main: (args) = +{ + a := 1; + b := 2; + c := 3; + d := 4; + + { + p : *int; + + // no initialization in selection_node + if args.argc > 20 { + c = 20; + } else if args.argc > 10 { + c = 10; + } else { + if args.argc % 2 { + b = 2; + } else { + b = 1; + } + } + + // initialization in first if condition + if p = b& { + p = a&; + } else if args.argc == 3 { + p = b&; + if args.argc == 2 { + p = c&; + } else { + if b > 0 { + p = a&; + } + else { + p = d&; + } + } + } else { + p = c&; + } + + std::cout << p* << std::endl; + } + + { + p : *int; + + // initialization in: + // - first if branch + // - second if condition + if args.argc == 3 { + p = a&; + } else if p = b& { + p = b&; + if args.argc == 2 { + p = c&; + } else { + if b > 0 { + p = a&; + } + else { + p = d&; + } + } + } else { + p = c&; + } + + std::cout << p* << std::endl; + } + + { + p : *int; + + // initialization in: + // - first if branch + // - second if branch in nested if condition (first if) + // - else branch + if args.argc == 3 { + p = a&; + } else if args.argc == 2 { + if p = b& { + p = c&; + } else { + if b > 0 { + p = a&; + } + else { + p = d&; + } + } + } else { + p = c&; + } + + std::cout << p* << std::endl; + } + + { + p : *int; + + // initialization in: + // - first if branch + // - second if branch in nested if: + // - first branch + // - second branch in nested if condition (first if) + // - else branch + if args.argc == 3 { + p = a&; + } else if args.argc == 2 { + if b > 0 { + p = c&; + } else { + if p = b& { + p = a&; + } + else { + p = d&; + } + p* = 42; + } + p* = 24; + } else { + p = c&; + } + + std::cout << p* << std::endl; + } + + { + p : *int; + + // initialization in: + // - first if branch + // - second if branch in nested if: + // - first branch + // - second branch in nested if: + // - first if branch + // - second if condition + // - else branch + if args.argc == 3 { + p = a&; + } else if args.argc == 2 { + if b > 0 { + p = c&; + } else { + if b > 2 { + p = a&; + } + else if p = b& { + d = p*; + } + else { + p = d&; + } + } + } else { + p = c&; + } + + std::cout << p* << std::endl; + } + +} diff --git a/regression-tests/pure2-nested-ifs-error.cpp2 b/regression-tests/pure2-nested-ifs-error.cpp2 new file mode 100644 index 000000000..8ff11f1f0 --- /dev/null +++ b/regression-tests/pure2-nested-ifs-error.cpp2 @@ -0,0 +1,27 @@ +main: (args) = { + p : *int; + + a := 1; + b := 2; + c := 3; + d := 4; + + if args.argc == 3 { + p = a&; + } else if b > 2 { + if args.argc == 2 { + p = c&; + } else { + if b > 0 { + p = a&; + } + else { + p = d&; + } + } + } else { + // p = c&; + } + + std::cout << p* << std::endl; +} \ No newline at end of file diff --git a/regression-tests/pure2-nested-ifs.cpp2 b/regression-tests/pure2-nested-ifs.cpp2 new file mode 100644 index 000000000..535f8a5a3 --- /dev/null +++ b/regression-tests/pure2-nested-ifs.cpp2 @@ -0,0 +1,52 @@ +main: (args) = +{ + a := 1; + b := 2; + c := 3; + d := 4; + + { + p : *int; + + // no initialization in selection_node + if args.argc > 20 { + c = 20; + } else if args.argc > 10 { + c = 10; + } else { + if args.argc % 2 { + b = 2; + } else { + b = 1; + } + } + + // initialization in: + // - first if branch + // - second if branch in nested if: + // - first branch + // - second branch in nested if: + // - first if branch + // - else branch + // - else branch + if args.argc == 3 { + p = a&; + } else if args.argc == 2 { + if b > 0 { + p = c&; + } else { + if b > 2 { + p = a&; + } + else { + p = d&; + } + } + } else { + p = c&; + } + + std::cout << p* << std::endl; + } + +} diff --git a/regression-tests/test-results/apple-clang-14/pure2-nested-ifs.cpp.execution b/regression-tests/test-results/apple-clang-14/pure2-nested-ifs.cpp.execution new file mode 100644 index 000000000..00750edc0 --- /dev/null +++ b/regression-tests/test-results/apple-clang-14/pure2-nested-ifs.cpp.execution @@ -0,0 +1 @@ +3 diff --git a/regression-tests/test-results/apple-clang-14/pure2-nested-ifs.cpp.output b/regression-tests/test-results/apple-clang-14/pure2-nested-ifs.cpp.output new file mode 100644 index 000000000..e69de29bb diff --git a/regression-tests/test-results/clang-12/pure2-nested-ifs.cpp.execution b/regression-tests/test-results/clang-12/pure2-nested-ifs.cpp.execution new file mode 100644 index 000000000..00750edc0 --- /dev/null +++ b/regression-tests/test-results/clang-12/pure2-nested-ifs.cpp.execution @@ -0,0 +1 @@ +3 diff --git a/regression-tests/test-results/clang-12/pure2-nested-ifs.cpp.output b/regression-tests/test-results/clang-12/pure2-nested-ifs.cpp.output new file mode 100644 index 000000000..e69de29bb diff --git a/regression-tests/test-results/gcc-10/pure2-nested-ifs.cpp.execution b/regression-tests/test-results/gcc-10/pure2-nested-ifs.cpp.execution new file mode 100644 index 000000000..00750edc0 --- /dev/null +++ b/regression-tests/test-results/gcc-10/pure2-nested-ifs.cpp.execution @@ -0,0 +1 @@ +3 diff --git a/regression-tests/test-results/gcc-10/pure2-nested-ifs.cpp.output b/regression-tests/test-results/gcc-10/pure2-nested-ifs.cpp.output new file mode 100644 index 000000000..e69de29bb diff --git a/regression-tests/test-results/msvc-2022/pure2-nested-ifs.cpp.execution b/regression-tests/test-results/msvc-2022/pure2-nested-ifs.cpp.execution new file mode 100644 index 000000000..00750edc0 --- /dev/null +++ b/regression-tests/test-results/msvc-2022/pure2-nested-ifs.cpp.execution @@ -0,0 +1 @@ +3 diff --git a/regression-tests/test-results/msvc-2022/pure2-nested-ifs.cpp.output b/regression-tests/test-results/msvc-2022/pure2-nested-ifs.cpp.output new file mode 100644 index 000000000..e69de29bb diff --git a/regression-tests/test-results/pure2-nested-ifs-conditions-error.cpp b/regression-tests/test-results/pure2-nested-ifs-conditions-error.cpp new file mode 100644 index 000000000..62f9c029d --- /dev/null +++ b/regression-tests/test-results/pure2-nested-ifs-conditions-error.cpp @@ -0,0 +1,187 @@ + +#define CPP2_USE_MODULES Yes + +//=== Cpp2 type declarations ==================================================== + + +#include "cpp2util.h" + + + +//=== Cpp2 type definitions and function declarations =========================== + +#line 1 "pure2-nested-ifs-conditions-error.cpp2" +auto main(int const argc_, char const* const* const argv_) -> int; + + +//=== Cpp2 function definitions ================================================= + +#line 1 "pure2-nested-ifs-conditions-error.cpp2" +auto main(int const argc_, char const* const* const argv_) -> int +{ + auto args = cpp2::make_args(argc_, argv_); +#line 3 "pure2-nested-ifs-conditions-error.cpp2" + auto a {1}; + auto b {2}; + auto c {3}; + auto d {4}; + + { + cpp2::deferred_init p; + + // no initialization in selection_node + if (cpp2::cmp_greater(args.argc,20)) { + c = 20; + } else if (cpp2::cmp_greater(args.argc,10)) { + c = 10; + }else { + if (args.argc % 2) { + b = 2; + }else { + b = 1; + } + } + + // initialization in first if condition + if (p.construct(&b)) { + p.value() = &a; + } else if (args.argc==3) { + p.value() = &b; + if (args.argc==2) { + p.value() = &c; + }else { + if (cpp2::cmp_greater(b,0)) { + p.value() = &a; + } + else { + p.value() = &d; + } + } + }else { + p.value() = &c; + } + + std::cout << *cpp2::assert_not_null(std::move(p.value())) << std::endl; + } + + { + cpp2::deferred_init p; + + // initialization in: + // - first if branch + // - second if condition + if (args.argc==3) { + p.construct(&a); + } else if (p.construct(&b)) { + p.value() = &b; + if (args.argc==2) { + p.value() = &c; + }else { + if (cpp2::cmp_greater(b,0)) { + p.value() = &a; + } + else { + p.value() = &d; + } + } + }else { + p.value() = &c; + } + + std::cout << *cpp2::assert_not_null(std::move(p.value())) << std::endl; + } + + { + cpp2::deferred_init p; + + // initialization in: + // - first if branch + // - second if branch in nested if condition (first if) + // - else branch + if (args.argc==3) { + p.construct(&a); + } else if (args.argc==2) { + if (p.construct(&b)) { + p.value() = &c; + }else { + if (cpp2::cmp_greater(b,0)) { + p.value() = &a; + } + else { + p.value() = &d; + } + } + }else { + p.construct(&c); + } + + std::cout << *cpp2::assert_not_null(std::move(p.value())) << std::endl; + } + + { + cpp2::deferred_init p; + + // initialization in: + // - first if branch + // - second if branch in nested if: + // - first branch + // - second branch in nested if condition (first if) + // - else branch + if (args.argc==3) { + p.construct(&a); + } else if (args.argc==2) { + if (cpp2::cmp_greater(b,0)) { + p.construct(&c); + }else { + if (p.construct(&b)) { + p.value() = &a; + } + else { + p.value() = &d; + } + *cpp2::assert_not_null(p.value()) = 42; + } + *cpp2::assert_not_null(p.value()) = 24; + }else { + p.construct(&c); + } + + std::cout << *cpp2::assert_not_null(std::move(p.value())) << std::endl; + } + + { + cpp2::deferred_init p; + + // initialization in: + // - first if branch + // - second if branch in nested if: + // - first branch + // - second branch in nested if: + // - first if branch + // - second if condition + // - else branch + if (args.argc==3) { + p.construct(&a); + } else if (args.argc==2) { + if (cpp2::cmp_greater(b,0)) { + p.construct(&c); + }else { + if (cpp2::cmp_greater(b,2)) { + p.construct(&a); + } + else if (p.construct(&b)) { + d = *cpp2::assert_not_null(p.value()); + } + else { + p.value() = &d; + } + } + }else { + p.construct(&c); + } + + std::cout << *cpp2::assert_not_null(std::move(p.value())) << std::endl; + } + +} + diff --git a/regression-tests/test-results/pure2-nested-ifs-conditions-error.cpp2.output b/regression-tests/test-results/pure2-nested-ifs-conditions-error.cpp2.output new file mode 100644 index 000000000..bffb6b27c --- /dev/null +++ b/regression-tests/test-results/pure2-nested-ifs-conditions-error.cpp2.output @@ -0,0 +1,2 @@ +pure2-nested-ifs-conditions-error.cpp2... ok (all Cpp2, passes safety checks) + diff --git a/regression-tests/test-results/pure2-nested-ifs-error.cpp2.output b/regression-tests/test-results/pure2-nested-ifs-error.cpp2.output new file mode 100644 index 000000000..9e33ea748 --- /dev/null +++ b/regression-tests/test-results/pure2-nested-ifs-error.cpp2.output @@ -0,0 +1,9 @@ +pure2-nested-ifs-error.cpp2... +pure2-nested-ifs-error.cpp2(2,5): error: local variable p must be initialized on both branches or neither branch +pure2-nested-ifs-error.cpp2(9,5): error: "if" initializes p on: + branch starting at line 9 + branch starting at line 11 +but not on: + branch starting at line 22 + ==> program violates initialization safety guarantee - see previous errors + diff --git a/regression-tests/test-results/pure2-nested-ifs.cpp b/regression-tests/test-results/pure2-nested-ifs.cpp new file mode 100644 index 000000000..f701f0dca --- /dev/null +++ b/regression-tests/test-results/pure2-nested-ifs.cpp @@ -0,0 +1,74 @@ + +#define CPP2_USE_MODULES Yes + +//=== Cpp2 type declarations ==================================================== + + +#include "cpp2util.h" + + + +//=== Cpp2 type definitions and function declarations =========================== + +#line 1 "pure2-nested-ifs.cpp2" +auto main(int const argc_, char const* const* const argv_) -> int; + + +//=== Cpp2 function definitions ================================================= + +#line 1 "pure2-nested-ifs.cpp2" +auto main(int const argc_, char const* const* const argv_) -> int +{ + auto args = cpp2::make_args(argc_, argv_); +#line 3 "pure2-nested-ifs.cpp2" + auto a {1}; + auto b {2}; + auto c {3}; + auto d {4}; + + { + cpp2::deferred_init p; + + // no initialization in selection_node + if (cpp2::cmp_greater(args.argc,20)) { + c = 20; + } else if (cpp2::cmp_greater(args.argc,10)) { + c = 10; + }else { + if (args.argc % 2) { + b = 2; + }else { + b = 1; + } + } + + // initialization in: + // - first if branch + // - second if branch in nested if: + // - first branch + // - second branch in nested if: + // - first if branch + // - else branch + // - else branch + if (args.argc==3) { + p.construct(&a); + } else if (args.argc==2) { + if (cpp2::cmp_greater(b,0)) { + p.construct(&c); + }else { + if (cpp2::cmp_greater(std::move(b),2)) { + p.construct(&a); + } + else { + p.construct(&d); + } + } + }else { + p.construct(&c); + } + + std::cout << *cpp2::assert_not_null(std::move(p.value())) << std::endl; + } + +} + diff --git a/regression-tests/test-results/pure2-nested-ifs.cpp2.output b/regression-tests/test-results/pure2-nested-ifs.cpp2.output new file mode 100644 index 000000000..2ac9b570c --- /dev/null +++ b/regression-tests/test-results/pure2-nested-ifs.cpp2.output @@ -0,0 +1,2 @@ +pure2-nested-ifs.cpp2... ok (all Cpp2, passes safety checks) +