forked from microsoft/proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy_integration_tests.cpp
138 lines (121 loc) · 4.02 KB
/
proxy_integration_tests.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include <gtest/gtest.h>
#include <iomanip>
#include <memory>
#include <memory_resource>
#include <numbers>
#include <sstream>
#include <string>
#include <vector>
#include "proxy.h"
namespace {
struct Draw : pro::dispatch<void(std::ostream&)> {
template <class T>
void operator()(const T& self, std::ostream& out) { self.Draw(out); }
};
struct Area : pro::dispatch<double()> {
template <class T>
double operator()(const T& self) { return self.Area(); }
};
struct DrawableFacade : pro::facade<Draw, Area> {};
class Rectangle {
public:
void Draw(std::ostream& out) const
{ out << "{Rectangle: width = " << width_ << ", height = " << height_ << "}"; }
void SetWidth(double width) { width_ = width; }
void SetHeight(double height) { height_ = height; }
double Area() const { return width_ * height_; }
private:
double width_;
double height_;
};
class Circle {
public:
void Draw(std::ostream& out) const { out << "{Circle: radius = " << radius_ << "}"; }
void SetRadius(double radius) { radius_ = radius; }
double Area() const { return std::numbers::pi * radius_ * radius_; }
private:
double radius_;
};
class Point {
public:
void Draw(std::ostream& out) const { out << "{Point}"; }
constexpr double Area() const { return 0; }
};
std::string PrintDrawableToString(pro::proxy<DrawableFacade> p) {
std::stringstream result;
result << std::fixed << std::setprecision(5) << "shape = ";
p.invoke<Draw>(result);
result << ", area = " << p.invoke<Area>();
return std::move(result).str();
}
std::vector<std::string> ParseCommand(const std::string& s) {
std::vector<std::string> result(1u);
bool in_quote = false;
std::size_t last_valid = s.find_last_not_of(' ');
for (std::size_t i = 0u; i <= last_valid; ++i) {
if (s[i] == '`' && i < last_valid) {
result.back() += s[++i];
} else if (s[i] == '"') {
in_quote = !in_quote;
} else if (s[i] == ' ' && !in_quote) {
if (!result.back().empty()) {
result.emplace_back();
}
} else {
result.back() += s[i];
}
}
if (result.back().empty()) {
result.pop_back();
}
return result;
}
pro::proxy<DrawableFacade> MakeDrawableFromCommand(const std::string& s) {
std::vector<std::string> parsed = ParseCommand(s);
if (!parsed.empty()) {
if (parsed[0u] == "Rectangle") {
if (parsed.size() == 3u) {
static std::pmr::unsynchronized_pool_resource rectangle_memory_pool;
std::pmr::polymorphic_allocator<> alloc{&rectangle_memory_pool};
auto deleter = [alloc](Rectangle* ptr) mutable
{ alloc.delete_object<Rectangle>(ptr); };
Rectangle* instance = alloc.new_object<Rectangle>();
std::unique_ptr<Rectangle, decltype(deleter)> p{instance, deleter};
p->SetWidth(std::stod(parsed[1u]));
p->SetHeight(std::stod(parsed[2u]));
return p;
}
} else if (parsed[0u] == "Circle") {
if (parsed.size() == 2u) {
Circle circle;
circle.SetRadius(std::stod(parsed[1u]));
return pro::make_proxy<DrawableFacade>(circle);
}
} else if (parsed[0u] == "Point") {
if (parsed.size() == 1u) {
static Point instance;
return &instance;
}
}
}
throw std::runtime_error{"Invalid command"};
}
} // namespace
TEST(ProxyIntegrationTests, TestDrawable) {
pro::proxy<DrawableFacade> p = MakeDrawableFromCommand("Rectangle 2 3");
std::string s = PrintDrawableToString(std::move(p));
ASSERT_EQ(s, "shape = {Rectangle: width = 2.00000, height = 3.00000}, area = 6.00000");
p = MakeDrawableFromCommand("Circle 1");
s = PrintDrawableToString(std::move(p));
ASSERT_EQ(s, "shape = {Circle: radius = 1.00000}, area = 3.14159");
p = MakeDrawableFromCommand("Point");
s = PrintDrawableToString(std::move(p));
ASSERT_EQ(s, "shape = {Point}, area = 0.00000");
try {
p = MakeDrawableFromCommand("Triangle 2 3");
} catch (const std::runtime_error& e) {
ASSERT_STREQ(e.what(), "Invalid command");
}
}