summaryrefslogtreecommitdiffhomepage
path: root/CompilerDriver/cc2/regression-tests/mixed-initialization-safety-3-contract-violation.cpp2
blob: 666fa9b22b40795ee93c34dd3bf570e2e7b0fd7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <random>
#include <string>
#include <vector>

main: () -> int = {
    std::set_terminate(std::abort);

    x: std::string;         // note: uninitialized!

    if flip_a_coin() {
        x = "xyzzy";
    } else {
        fill( out x, "plugh", 40 ); // note: constructs x!
    }
    print_decorated(x);
}

fill: (
    out x: std::string,
    value: std::string,
    count: int
    )
    pre( value.ssize() >= count, "fill: value must contain at least count elements" )
= {
    x = value.substr(0, count);
}

print_decorated: (x:_) = std::cout << ">> [" << x << "]\n";

// for test determinism, force "fill" branch
bool flip_a_coin() {
    // Change std::mt19937 to std::random_device for non-deterministic PRNG
    static std::mt19937 rand;
    return rand() % 2 == 1;
}