-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip.cpp
48 lines (36 loc) · 971 Bytes
/
ip.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
#include "ip.h"
#include <cstdio>
Ip::Ip(const std::string r) {
unsigned int a, b, c, d;
int res = sscanf(r.c_str(), "%u.%u.%u.%u", &a, &b, &c, &d);
if (res != SIZE) {
fprintf(stderr, "Ip::Ip sscanf return %d r=%s\n", res, r.c_str());
return;
}
ip_ = (a << 24) | (b << 16) | (c << 8) | d;
}
Ip::operator std::string() const {
char buf[32]; // enough size
sprintf(buf, "%u.%u.%u.%u",
(ip_ & 0xFF000000) >> 24,
(ip_ & 0x00FF0000) >> 16,
(ip_ & 0x0000FF00) >> 8,
(ip_ & 0x000000FF));
return std::string(buf);
}
#ifdef GTEST
#include <gtest/gtest.h>
TEST(Ip, ctorTest) {
Ip ip1; // Ip()
Ip ip2(0x7F000001); // Ip(const uint32_t r)
Ip ip3("127.0.0.1"); // Ip(const std::string r);
EXPECT_EQ(ip2, ip3);
}
TEST(Ip, castingTest) {
Ip ip("127.0.0.1");
uint32_t ui = ip; // operator uint32_t() const
EXPECT_EQ(ui, 0x7F000001);
std::string s = std::string(ip); // explicit operator std::string()
EXPECT_EQ(s, "127.0.0.1");
}
#endif // GTEST