forked from asaidalaoui/cs371p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shapes2.c++
127 lines (102 loc) · 3.32 KB
/
Shapes2.c++
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
// -----------
// Shapes2.c++
// -----------
#include <cassert> // assert
#include <iostream> // cout, endl, istream, ostream
#include <utility> // !=
#include "gtest/gtest.h"
using namespace std;
using rel_ops::operator!=;
class Shape {
friend bool operator == (const Shape& lhs, const Shape& rhs) {
return lhs.equals(rhs);}
friend std::istream& operator >> (std::istream& lhs, Shape& rhs) {
return rhs.read(lhs);}
friend std::ostream& operator << (std::ostream& lhs, const Shape& rhs) {
return rhs.write(lhs);}
private:
int _x;
int _y;
protected:
virtual bool equals (const Shape& rhs) const {
return (_x == rhs._x) && (_y == rhs._y);}
virtual std::istream& read (std::istream& in) {
return in >> _x >> _y;}
virtual std::ostream& write (std::ostream& out) const {
return out << _x << " " << _y;}
public:
Shape (int x, int y) :
_x (x),
_y (y)
{}
Shape (const Shape&) = default;
virtual ~Shape ()
{}
Shape& operator = (const Shape&) = default;
virtual double area () const {
return 0;}
void move (int x, int y) {
_x = x;
_y = y;}};
#include "Shapes2.h"
TEST(ShapeFixture, test_1) {
Shape x(2, 3);
x.move(4, 5);
ASSERT_EQ(0, x.area());
// x.radius(); // doesn't compile
}
TEST(ShapeFixture, test_2) {
const Shape x(2, 3);
Shape y(4, 5);
ASSERT_NE(x, y);
y = x;
ASSERT_EQ(x, y);}
TEST(ShapeFixture, test_3) {
Circle x(2, 3, 4);
x.move(5, 6);
ASSERT_EQ(3.14 * 4 * 4, x.area());
ASSERT_EQ(4, x.radius());}
TEST(ShapeFixture, test_4) {
const Circle x(2, 3, 4);
Circle y(2, 3, 5);
ASSERT_NE(x, y);
y = x;
ASSERT_EQ(x, y);}
TEST(ShapeFixture, test_5) {
// Circle* const p = new Shape(2, 3); // doesn't compile
Shape* const p = new Circle(2, 3, 4);
p->move(5, 6);
ASSERT_EQ(3.14 * 4 * 4, p->area());
// p->radius(); // doesn't compile
if (Circle* const q = dynamic_cast<Circle*>(p))
ASSERT_EQ(4, q->radius());
try {
Circle& r = dynamic_cast<Circle&>(*p);
ASSERT_EQ(4, r.radius());}
catch (const bad_cast& e) {
ASSERT_TRUE(false);}
delete p;}
TEST(ShapeFixture, test_6) {
const Shape* const p = new Circle(2, 3, 4);
Shape* const q = new Circle(2, 3, 5);
ASSERT_NE(*p, *q);
// *q = *p; // illdefined
if (const Circle* const r = dynamic_cast<const Circle*>(p))
if (Circle* const s = dynamic_cast<Circle*>(q))
*s = *r;
ASSERT_EQ(*p, *q);
delete p;
delete q;}
TEST(ShapeFixture, test_7) {
// const Circle a[] = {Shape(2, 3), Circle(4, 5, 6)}; // doesn't compile
const Shape a[] = {Shape(2, 3), Circle(4, 5, 6)};
ASSERT_EQ(0, a[0].area());
ASSERT_EQ(0, a[1].area());}
TEST(ShapeFixture, test_8) {
const Circle a[] = {Circle(2, 3, 4), Circle(5, 6, 7)};
ASSERT_EQ(3.14 * 4 * 4, a[0].area());
ASSERT_EQ(3.14 * 7 * 7, a[1].area());
const Shape* const p = a;
ASSERT_EQ(3.14 * 4 * 4, p[0].area());
// p[1].area(); // illdefined
}