Skip to content

Instantly share code, notes, and snippets.

@jpcofr
Created October 1, 2023 09:31
Show Gist options
  • Save jpcofr/ce4ff82fd88f7f5c382675e0d1037345 to your computer and use it in GitHub Desktop.
Save jpcofr/ce4ff82fd88f7f5c382675e0d1037345 to your computer and use it in GitHub Desktop.
Demonstrating Comparison Usage with std::sort using Google Test
cmake_minimum_required(VERSION 3.25)
set(CMAKE_CXX_STANDARD 11)
project(c_cpp_snippet)
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Find ccache
find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM)
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK "${CCACHE_PROGRAM}")
endif()
## Configure testing
include(FetchContent)
set(FETCHCONTENT_QUIET TRUE)
# Download and configure googletest
FetchContent_Declare(
gtest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG v1.13.0
GIT_SHALLOW ON
)
FetchContent_GetProperties(gtest)
if(NOT gtest_POPULATED)
FetchContent_Populate(gtest)
add_subdirectory(${gtest_SOURCE_DIR} ${CMAKE_BINARY_DIR}/_deps/gtest-build)
endif()
include_directories(${gtest_SOURCE_DIR}/googletest/include)
## Configure executables
add_executable(test main.cpp)
target_link_libraries(test PRIVATE gtest_main)
#include <gtest/gtest.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
// ****************************************************************************
// This tests showcase the usage of comparisons with std::sort
// ****************************************************************************
typedef std::pair<std::string, int> Entry;
void print_table(const std::vector<Entry>& table, std::string legend) {
std::cout << std::string(80, '=') << std::endl;
std::cout << legend << std::endl;
for (auto& host : table)
std::cout << host.first << " " << host.second << std::endl;
std::cout << std::string(80, '=') << std::endl;
}
void fill_table(std::vector<Entry>& table) {
table.push_back(std::make_pair("sa.bbc.com", 2));
table.push_back(std::make_pair("stats.g.doubleclick.net", 2));
table.push_back(std::make_pair("sa.bbc.co.uk", 3));
table.push_back(std::make_pair("px.dynamicyield.com", 2));
table.push_back(std::make_pair("www.calcalist.co.il", 2));
table.push_back(std::make_pair("www.ynet.co.il", 9));
table.push_back(std::make_pair("edigitalsurvey.com", 1));
table.push_back(std::make_pair("static.chartbeat.com", 2));
table.push_back(std::make_pair("server.exposebox.com", 14));
table.push_back(std::make_pair("node1.bbcimg.co.uk", 3));
table.push_back(std::make_pair("news.bbcimg.co.uk", 2));
table.push_back(std::make_pair("a.visualrevenue.com", 1));
table.push_back(std::make_pair("r.bbci.co.uk", 1));
table.push_back(std::make_pair("static.bbci.co.uk", 30));
table.push_back(std::make_pair("b.scorecardresearch.com", 1));
table.push_back(std::make_pair("clients1.google.com", 2));
table.push_back(std::make_pair("open.live.bbc.co.uk", 1));
table.push_back(std::make_pair("xnet.ynet.co.il", 2));
table.push_back(std::make_pair("p.visualrevenue.com", 1));
table.push_back(std::make_pair("emp.bbci.co.uk", 2));
table.push_back(std::make_pair("sf.exposebox.com", 9));
table.push_back(std::make_pair("dy.ynet.co.il", 1));
table.push_back(std::make_pair("s2.googleusercontent.com", 8));
table.push_back(std::make_pair("s3-eu-west-1.amazonaws.com", 1));
table.push_back(std::make_pair("t1.visualrevenue.com", 1));
table.push_back(std::make_pair("exposebox.blob.core.windows.net", 1));
table.push_back(std::make_pair("bbc.com", 1));
table.push_back(std::make_pair("go.ynet.co.il", 1));
table.push_back(std::make_pair("ichef.bbci.co.uk", 29));
table.push_back(std::make_pair("googleads.g.doubleclick.net", 1));
table.push_back(std::make_pair("www.bbc.com", 1));
table.push_back(std::make_pair("www.google-analytics.com", 1));
table.push_back(std::make_pair("www.google.co.il", 2));
table.push_back(std::make_pair("ocsp.startssl.com", 1));
table.push_back(std::make_pair("www.bbc.co.uk", 2));
table.push_back(std::make_pair("survey.112.2o7.net", 1));
table.push_back(std::make_pair("www.google.com", 2));
table.push_back(std::make_pair("st.dynamicyield.com", 1));
table.push_back(std::make_pair("cdn.clicktale.net", 1));
table.push_back(std::make_pair("ping.chartbeat.net", 2));
table.push_back(std::make_pair("www.googleadservices.com", 1));
table.push_back(std::make_pair("static.bbc.co.uk", 1));
}
TEST(std_sort, my_change) {
std::vector<Entry> table;
fill_table(table);
print_table(table, "==== Before sort");
auto hostnameComparer = [](const Entry& leftHost, const Entry& rightHost) {
return leftHost.second > rightHost.second ||
(leftHost.second == rightHost.second &&
leftHost.first > rightHost.first);
};
std::sort(table.begin(), table.end(), hostnameComparer);
print_table(table, "==== My change after sort...");
SUCCEED();
}
TEST(std_sort, his_change) {
std::vector<Entry> table;
fill_table(table);
print_table(table, "==== Before sort");
auto hostnameComparer = [](const Entry& leftHost, const Entry& rightHost) {
return leftHost.second < rightHost.second ||
(leftHost.second == rightHost.second &&
leftHost.first < rightHost.first);
};
std::sort(table.begin(), table.end(), hostnameComparer);
print_table(table, "==== His change after sort...");
SUCCEED();
}
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
----------------------------------------------------
| Hostname | Count |
----------------------------------------------------
| static.bbci.co.uk | 30 |
| ichef.bbci.co.uk | 29 |
| server.exposebox.com | 14 |
| www.ynet.co.il | 9 |
| sf.exposebox.com | 9 |
| s2.googleusercontent.com | 8 |
| sa.bbc.co.uk | 3 |
| node1.bbcimg.co.uk | 3 |
| xnet.ynet.co.il | 2 |
| www.google.com | 2 |
| www.google.co.il | 2 |
| www.calcalist.co.il | 2 |
| www.bbc.co.uk | 2 |
| stats.g.doubleclick.net | 2 |
| static.chartbeat.com | 2 |
| sa.bbc.com | 2 |
| px.dynamicyield.com | 2 |
| ping.chartbeat.net | 2 |
| news.bbcimg.co.uk | 2 |
| emp.bbci.co.uk | 2 |
| clients1.google.com | 2 |
| www.googleadservices.com | 1 |
| www.google-analytics.com | 1 |
| www.bbc.com | 1 |
| t1.visualrevenue.com | 1 |
| survey.112.2o7.net | 1 |
| static.bbc.co.uk | 1 |
| st.dynamicyield.com | 1 |
| s3-eu-west-1.amazonaws.com | 1 |
| r.bbci.co.uk | 1 |
| p.visualrevenue.com | 1 |
| open.live.bbc.co.uk | 1 |
| ocsp.startssl.com | 1 |
| googleads.g.doubleclick.net | 1 |
| go.ynet.co.il | 1 |
| exposebox.blob.core.windows.net | 1 |
| edigitalsurvey.com | 1 |
| dy.ynet.co.il | 1 |
| cdn.clicktale.net | 1 |
| bbc.com | 1 |
| b.scorecardresearch.com | 1 |
| a.visualrevenue.com | 1 |
----------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment