-
Notifications
You must be signed in to change notification settings - Fork 0
/
mac.cpp
56 lines (44 loc) · 1.12 KB
/
mac.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
#include "mac.h"
Mac::Mac(const std::string r) {
unsigned int a, b, c, d, e, f;
int res = sscanf(r.c_str(), "X:X:X:X:X:X", &a, &b, &c, &d, &e, &f);
if (res != SIZE) {
fprintf(stderr, "Mac::Mac sscanf return %d r=%s\n", res, r.c_str());
return;
}
mac_[0] = a;
mac_[1] = b;
mac_[2] = c;
mac_[3] = d;
mac_[4] = e;
mac_[5] = f;
}
Mac::operator std::string() const {
char buf[32]; // enough size
sprintf(buf, "x:X:X:X:X:X",
mac_[0],
mac_[1],
mac_[2],
mac_[3],
mac_[4],
mac_[5]);
return std::string(buf);
}
#ifdef GTEST
#include <gtest/gtest.h>
TEST(Mac, ctorTest) {
Mac mac1; // Mac()
uint8_t buf[] = {0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC};
Mac mac2(buf); // Mac(const std::string r)
Mac mac3("12:34:56:78:9A:BC"); // Mac(const std::string r)
EXPECT_EQ(mac2, mac3);
}
TEST(Mac, castingTest) {
Mac mac("12:34:56:78:9A:BC");
uint8_t buf[Mac::SIZE];
memcpy(buf, mac, Mac::SIZE); // operator uint8_t*()
EXPECT_TRUE(memcmp(buf, mac, Mac::SIZE) == 0);
std::string s = std::string(mac); // explicit operator std::string()
EXPECT_EQ(s, "12:34:56:78:9A:BC");
}
#endif // GTEST