GaitGeneration by Graph Search
読み取り中…
検索中…
一致する文字列を見つけられません
my_unexpected_test.h
[詳解]
1
3
4// Copyright(c) 2023-2025 Design Engineering Laboratory, Saitama University
5// Released under the MIT license
6// https://opensource.org/licenses/mit-license.php
7
8#ifndef DESIGNLAB_MY_UNEXPECTED_TEST_H_
9#define DESIGNLAB_MY_UNEXPECTED_TEST_H_
10
11#include <doctest.h>
12
13#include <string>
14
15#include "my_unexpected.h"
16
17TEST_SUITE("unexpected") {
19
20 TEST_CASE("unexpected construction") {
21 // 単一の変数によるコンストラクタ
22
23 SUBCASE("when E is a integral type, it should be constructible") {
24 unexpected<int> u(42);
25 CHECK_EQ(u.error(), 42);
26 }
27
28 SUBCASE("when E is a string, it should be constructible") {
29 unexpected<std::string> u("error");
30 CHECK_EQ(u.error(), "error");
31 }
32
33 SUBCASE("when E is a custom type, it should be constructible") {
34 struct CustomError {
35 int code;
36 std::string message;
37 };
38 unexpected<CustomError> u(CustomError{404, "Not Found"});
39 CHECK_EQ(u.error().code, 404);
40 CHECK_EQ(u.error().message, "Not Found");
41 }
42 }
43
44 // 任意個の引数を取るコンストラクタ
45 TEST_CASE("unexpected with in_place_t") {
46 struct CustomError {
47 int code;
48 std::string message;
49 };
50
51 unexpected<CustomError> u(std::in_place, 500, "Internal Server Error");
52 CHECK_EQ(u.error().code, 500);
53 CHECK_EQ(u.error().message, "Internal Server Error");
54 }
55
56 TEST_CASE("unexpected copy construction") {
57 unexpected<int> u1(42);
58 unexpected<int> u2 = u1; // copy
59 CHECK_EQ(u2.error(), 42);
60 }
61
62 TEST_CASE("unexpected move construction") {
63 unexpected<int> u1(42);
64 unexpected<int> u2 = std::move(u1); // move
65 CHECK_EQ(u2.error(), 42);
66 }
67
68 TEST_CASE("unexpected assignment") {
69 unexpected<int> u1(42);
70 unexpected<int> u2(100);
71 u2 = u1; // copy assignment
72 CHECK_EQ(u2.error(), 42);
73
74 unexpected<int> u3(200);
75 u3 = std::move(u1); // move assignment
76 CHECK_EQ(u3.error(), 42);
77 }
78
79 TEST_CASE("unexpected equality comparison") {
80 unexpected<int> u1(42);
81 unexpected<int> u2(42);
82 unexpected<int> u3(100);
83
84 CHECK(u1 == u2); // should be equal
85 CHECK_FALSE(u1 == u3); // should not be equal
86 CHECK_FALSE(u2 == u3); // should not be equal
87 }
88
89 TEST_CASE("unexpected inequality comparison") {
90 unexpected<int> u1(42);
91 unexpected<int> u2(42);
92 unexpected<int> u3(100);
93
94 CHECK_FALSE(u1 != u2); // should not be unequal
95 CHECK(u1 != u3); // should be unequal
96 CHECK(u2 != u3); // should be unequal
97 }
98
99 TEST_CASE("unexpected error access") {
100 unexpected<std::string> u("error");
101 CHECK_EQ(u.error(), "error");
102
103 // エラーを参照する
104 std::string& err_ref = u.error();
105 err_ref = "new error";
106 CHECK_EQ(u.error(), "new error");
107
108 // ムーブしてもエラーは保持される
109 unexpected<std::string> u2 = std::move(u);
110 CHECK_EQ(u2.error(), "new error");
111 }
112}
113
114#endif // DESIGNLAB_MY_UNEXPECTED_TEST_H_
TEST_SUITE("unexpected")