//model/book.h #pragma once #include <iostream> using namespace std; class book { public: book() = default; ~book() = default; book(const book &bk) = default; book(const book &&bk) = delete; void set_idx(const uint64_t &idxvalue); void set_id(const uint64_t &idvalue); void set_abstract(const string &abstract); void set_author(const string &author); void set_comment(const string &comment); void set_content(const string &content); void set_isbn(const string &isbn); void set_name(const string &name); void set_summary(const string &summary); void set_title(const string &title); void set_topic(const string &topic); uint64_t get_idx(); uint64_t get_id(); string get_abstract(); string get_author(); string get_comment(); string get_content(); string get_isbn(); string get_name(); string get_summary(); string get_title(); string get_topic(); private: uint64_t _idx; uint64_t _id; string _abstract; string _author; string _comment; string _content; string _isbn; string _name; string _summary; string _title; string _topic; }; //model/book.cpp #include "model/book.h" void book::set_idx(const uint64_t &idx) { _idx = idx; } void book::set_id(const uint64_t &id) { _id = id; } void book::set_abstract(const string &abstract) { _abstract = abstract; } void book::set_author(const string &author) { _author = author; } void book::set_comment(const string &comment) { _comment = comment; } void book::set_content(const string &content) { _content = content; } void book::set_isbn(const string &isbn) { _isbn = isbn; } void book::set_name(const string &name) { _name = name; } void book::set_summary(const string &summary) { _summary = summary; } void book::set_title(const string &title) { _title = title; } void book::set_topic(const string &topic) { _topic = topic; } uint64_t book::get_idx() { return _idx; } uint64_t book::get_id() { return _id; } string book::get_abstract() { return _abstract; } string book::get_author() { return _author; } string book::get_comment() { return _comment; } string book::get_content() { return _content; } string book::get_isbn() { return _isbn; } string book::get_name() { return _name; } string book::get_summary() { return _summary; } string book::get_title() { return _title; } string book::get_topic() { return _topic; } //model/util.h #pragma once #ifndef __util_h__ #define __util_h__ #include <chrono> #include <ctime> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <sstream> #include <thread> #include <unistd.h> #include <uuid/uuid.h> #include <vector> #include <jsoncpp/json/json.h> #include "model/book.h" class util { public: util() = default; ~util() = default; // forbide copy constructor util(const util &ul) = delete; // forbide move constructor util(const util &&ul) = delete; string get_time_now(); string get_uuid(); void fill_book_vector(std::vector<book> &vec, int len); void print_book_vector(std::vector<book> &vec); void log_file_msg(const string &filename, const string &jsonvalue); void serialize_book_vector(std::vector<book> &vec); void json_book_vector(const int &len); void thread_le(int x, int y, int z, const std::string &str); ; }; #endif //model/util.cpp #include "model/util.h" void util::log_file_msg(const string &filename, const string &jsonvalue) { fstream wfile(filename,ios::out); if(!wfile.good()) { cout<<get_time_now()<<",create "<<filename<<" failed"<<endl; return; } wfile<<jsonvalue; if(!wfile.good()) { cout<<get_time_now()<<",write into file "<<filename<<" failed!"<<endl; return; } wfile.close(); cout<<get_time_now()<<",write into file "<<filename<<" successfully!"<<endl; } void util::thread_le(int x, int y, int z, const std::string &str) { thread t1([](int xx, int yy, int zz, string sstr) { util ul; for(int i=0;i<xx;i++) { cout<<i<<","<<ul.get_time_now()<<","<<ul.get_uuid()<<endl; usleep(100000); } for(int i=0;i<yy;i++) { cout<<i<<","<<ul.get_uuid()<<","<<ul.get_time_now()<<endl; usleep(100000); } for(int i=0;i<zz;i++) { cout<<ul.get_time_now()<<","<<i<<","<<ul.get_uuid()<<endl; usleep(100000); } cout<<endl<<sstr<<endl; }, x, y, z, str); t1.join(); cout << get_time_now() << ",finished in " << __FUNCTION__ << "," << __LINE__ << endl; } void util::json_book_vector(const int &len) { vector<book> vec; fill_book_vector(std::ref(vec), len); serialize_book_vector(std::ref(vec)); cout << get_time_now() << ",finished in " << __FUNCTION__ << "," << __LINE__ << endl; } void util::serialize_book_vector(std::vector<book> &vec) { Json::Value root; vector<book>::iterator itr = vec.begin(); while (itr != vec.end()) { Json::Value bkjson; bkjson["id"] = itr->get_idx(); bkjson["idx"] = itr->get_id(); bkjson["abstract"] = itr->get_abstract(); bkjson["author"] = itr->get_author(); bkjson["comment"] = itr->get_comment(); bkjson["content"] = itr->get_content(); bkjson["isbn"] = itr->get_isbn(); bkjson["name"] = itr->get_name(); bkjson["summary"] = itr->get_summary(); bkjson["title"] = itr->get_title(); bkjson["topic"] = itr->get_topic(); root.append(bkjson); ++itr; } Json::StyledWriter writer; string jsonvalue = writer.write(std::ref(root)); string filename(get_time_now()); filename.append(".txt"); log_file_msg(filename,jsonvalue); cout << endl << get_time_now() << ",finished in " << __FUNCTION__ << "," << __LINE__ << endl; } void util::print_book_vector(std::vector<book> &vec) { vector<book>::iterator itr = vec.begin(); while (itr != vec.end()) { cout<<(*itr).get_idx()<<endl; cout<<fixed<<(*itr).get_idx(); cout<<fixed <<itr->get_idx() << "," << itr->get_id() << "," << itr->get_abstract() << "," << itr->get_author() << "," << itr->get_comment() << "," << itr->get_content() << "," << itr->get_isbn() << "," << itr->get_name() << "," << itr->get_summary() << "," << itr->get_title() << "," << itr->get_topic() << endl; ++itr; } } void util::fill_book_vector(std::vector<book> &vec, int len) { for (int i = 0; i < len; i++) { book bk; bk.set_idx(static_cast<std::uint64_t>(i+1)); bk.set_id(static_cast<uint64_t>(i*i*i*i*i)); bk.set_abstract(get_uuid()); bk.set_author(get_uuid()); bk.set_comment(get_uuid()); bk.set_content(get_uuid()); bk.set_isbn(get_uuid()); bk.set_name(get_uuid()); bk.set_summary(get_uuid()); bk.set_title(get_uuid()); bk.set_topic(get_uuid()); vec.emplace_back(bk); } cout << get_time_now() << ",finished in " << __FUNCTION__ << "," << __LINE__ << endl; } string util::get_time_now() { chrono::time_point<chrono::high_resolution_clock> now = chrono::high_resolution_clock::now(); time_t rawtime = chrono::high_resolution_clock::to_time_t(now); struct tm tminfo = *localtime(&rawtime); chrono::milliseconds mills = chrono::duration_cast<chrono::milliseconds>(now.time_since_epoch()); chrono::seconds seconds = chrono::duration_cast<chrono::seconds>(now.time_since_epoch()); int64_t millscount = mills.count() - seconds.count() * 1000; stringstream ss; ss << std::put_time(&tminfo, "%Y%m%d%H%M%S") << std::setw(3) << std::setfill('0') << millscount; string dtstr(ss.str()); ss = stringstream(); return dtstr; } string util::get_uuid() { uuid_t newuuid; uuid_generate(newuuid); char *uuidvalue = (char *)malloc(40); uuid_unparse(newuuid, uuidvalue); string uuidstr(uuidvalue); free(uuidvalue); uuidvalue = nullptr; return uuidstr; } //test/book_test.cpp #include <gtest/gtest.h> #include "model/book.h" #define private public #include "model/book.h" #undef private TEST(book_test,get_idx_test) { book _bk; uint64_t _idx=static_cast<uint64_t>(100*100*100*100); _bk.set_idx(_idx); ASSERT_EQ(_idx,_bk.get_idx()); } TEST(book_test,get_id_test) { book _bk; uint64_t _id=static_cast<uint64_t>(100*100*100*100*100); _bk.set_id(_id); ASSERT_EQ(_id,_bk.get_id()); } TEST(book_test,get_abstract_test) { book _bk; string _abstract="Master C++"; _bk.set_abstract(_abstract); ASSERT_EQ(_abstract,_bk.get_abstract()); } TEST(book_test,get_author_test) { book _bk; string _author("Bjarne Stroustrup"); _bk.set_author(_author); ASSERT_EQ(_author,_bk.get_author()); } TEST(book_test,get_comment_test) { book _bk; string _comment("learing c++ step by step"); _bk.set_comment(_comment); ASSERT_EQ(_comment,_bk.get_comment()); } TEST(book_test,get_content_test) { book _bk; string _content("master c++ line by line"); _bk.set_content(_content); ASSERT_EQ(_content,_bk.get_content()); } TEST(book_test,get_isbn_test) { book _bk; string _isbn("c++ isbn"); _bk.set_isbn(_isbn); ASSERT_EQ(_isbn,_bk.get_isbn()); } TEST(book_test,get_name_test) { book _bk; string _name("C++ principles"); _bk.set_name(_name); ASSERT_EQ(_name,_bk.get_name()); } TEST(book_test,get_summary_test) { book _bk; string _summary("C++ learing feedback!"); _bk.set_summary(_summary); ASSERT_EQ(_summary,_bk.get_summary()); } TEST(book_test,get_title_test) { book _bk; string _title("C++ STL step by step"); _bk.set_title(_title); ASSERT_EQ(_title,_bk.get_title()); } TEST(book_test,get_topic_test) { book _bk; string _topic("learing c++ chapter by chapter!"); _bk.set_topic(_topic); ASSERT_EQ(_topic,_bk.get_topic()); } //test/main.cpp #include <gtest/gtest.h> int main(int args,char**argv) { testing::InitGoogleTest(&args,argv); return::RUN_ALL_TESTS(); }
Compile the unit test
g++ -g -std=c++2a -I. ./model/*.cpp ./test/*.cpp -o h1 -luuid -lpthread -ljsoncpp /usr/lib/libgtest.a
Run
./h1
Compile the normal pragram
g++ -g -std=c++2a -I. *.cpp ./model/*.cpp -o h1 -luuid -lpthread -ljsoncpp /usr/lib/libgtest.a
Run
./h1 100
标签:via,set,string,get,bk,gtest,book,test,const From: https://www.cnblogs.com/Fred1987/p/17045257.html