overflow.cpp

 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <felspar/test.hpp>
#include <felspar/exceptions.hpp>
#include <iostream>
#include <vector>


namespace {


    auto const suite = felspar::testsuite("overflow");


    auto const c = suite.test("construct", []() {
        felspar::stdexcept::overflow_error{
                "Some text", felspar::source_location::current()};
        felspar::stdexcept::overflow_error{
                "Some text", 4.5, felspar::source_location::current()};
        felspar::stdexcept::overflow_error{
                "Some text", 4.5, 3.0, felspar::source_location::current()};
    });


    auto const v = suite.test("void", [](auto check) {
        check([]() {
            throw felspar::stdexcept::overflow_error{std::string{"Some error"}};
        }).throws(felspar::stdexcept::overflow_error{"Some error"});
        check([]() { throw felspar::stdexcept::overflow_error{"Some error"}; })
                .throws(felspar::stdexcept::overflow_error{
                        std::string{"Some error"}});
    });


    auto const i = suite.test("int", [](auto check) {
        check([]() {
            throw felspar::stdexcept::overflow_error{
                    std::string{"Some error"}, 3};
        }).throws(felspar::stdexcept::overflow_error<int>{"Some error"});
        check([]() {
            throw felspar::stdexcept::overflow_error{
                    std::string{"Wrong number of wheels"}, 5, 4};
        })
                .throws(felspar::stdexcept::overflow_error<int>{
                        "Wrong number of wheels"});
    });


    template<typename V>
    V const &at(
            std::vector<V> const &v,
            std::size_t const pos,
            felspar::source_location loc = felspar::source_location::current()) {
        if (pos >= v.size()) {
            throw felspar::stdexcept::overflow_error{
                    "Requested index beyond vector bound", pos, v.size(),
                    std::move(loc)};
        } else {
            return v[pos];
        }
    }
    auto const a = suite.test("at", [](auto check) {
        auto const loc = felspar::source_location::current();
        auto eptr = check([&]() {
                        std::vector<int> v;
                        at(v, 3, loc);
                    })
                            .throws(std::overflow_error{
                                    "Requested index beyond vector bound"});
        try {
            std::rethrow_exception(eptr);
        } catch (felspar::stdexcept::overflow_error<std::size_t> const &e) {
            check(e.thrown_from().line()) == loc.line();
        }
    });


}