-
Notifications
You must be signed in to change notification settings - Fork 0
/
pb_leak.cc
92 lines (65 loc) · 1.86 KB
/
pb_leak.cc
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
#include <iostream>
#include <string>
#include "addressbook.pb.h"
void test1()
{
tutorial::AddressBook address_book;
for (std::size_t i = 0; i < 100; ++i) {
tutorial::AddressBook next_address_book;
for (std::size_t j = 0; j < 100; ++j) {
auto person = next_address_book.add_people();
person->set_name(std::to_string(j));
person->set_id(j);
for (std::size_t k = 0; k < j; ++k)
person->add_phones()->set_number(std::to_string(k));
}
auto address_book_copy = next_address_book;
if (address_book_copy.people_size() < 1)
break;
address_book = address_book_copy;
}
}
void test2()
{
auto address_book = std::make_shared<tutorial::AddressBook>();
for (std::size_t i = 0; i < 100; ++i) {
auto next_address_book = std::make_shared<tutorial::AddressBook>(*address_book);
for (std::size_t j = 0; j < 100; ++j) {
auto person = next_address_book->add_people();
person->set_name(std::to_string(j));
person->set_id(j);
for (std::size_t k = 0; k < j; ++k)
person->add_phones()->set_number(std::to_string(k));
}
auto address_book_copy = next_address_book;
if (address_book_copy->people_size() < 1)
break;
address_book = address_book_copy;
}
}
// Main function: Reads the entire address book from a file,
// adds one person based on user input, then writes it back out to the same
// file.
int main(int argc, char* argv[])
{
GOOGLE_PROTOBUF_VERIFY_VERSION;
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " TEST TO RUN { 1, 2 }" << std::endl;
return -1;
}
switch(std::stoul(argv[1])) {
case 1:
std::cout << ">>> main running test 1" << std::endl;
test1();
break;
case 2:
std::cout << ">>> main running test 2" << std::endl;
test2();
break;
default:
break;
}
// Optional: Delete all global objects allocated by libprotobuf.
google::protobuf::ShutdownProtobufLibrary();
return 0;
}