|
| 1 | +#include "server/header_size.hpp" |
| 2 | + |
| 3 | +#include <boost/test/test_tools.hpp> |
| 4 | +#include <boost/test/unit_test.hpp> |
| 5 | + |
| 6 | +#include <string> |
| 7 | + |
| 8 | +std::string generateWorstCaseRequest(unsigned num_coordinates) |
| 9 | +{ |
| 10 | + if (num_coordinates == 0) |
| 11 | + return "/table/v1/driving/"; |
| 12 | + |
| 13 | + std::string request; |
| 14 | + request.reserve(50 * num_coordinates); |
| 15 | + request += "/table/v1/driving/"; |
| 16 | + |
| 17 | + // 1. Coordinates: (2-3 (major) + 1 (dot) + 6 (decimals)) * 2 = 20 chars per coordinate |
| 18 | + for (unsigned i = 0; i < num_coordinates; ++i) |
| 19 | + { |
| 20 | + request += "179.123456,89.123456"; |
| 21 | + if (i < num_coordinates - 1) |
| 22 | + request += ";"; |
| 23 | + } |
| 24 | + |
| 25 | + request += "?"; |
| 26 | + |
| 27 | + // 2. Radiuses: 3 digits |
| 28 | + request += "radiuses="; |
| 29 | + for (unsigned i = 0; i < num_coordinates; ++i) |
| 30 | + { |
| 31 | + request += "999"; |
| 32 | + if (i < num_coordinates - 1) |
| 33 | + request += ";"; |
| 34 | + } |
| 35 | + request += "&"; |
| 36 | + |
| 37 | + // 3. Sources and Destinations: 4 digits |
| 38 | + std::string sources_str = "sources="; |
| 39 | + std::string destinations_str = "destinations="; |
| 40 | + std::string index_str = ""; |
| 41 | + for (unsigned i = 0; i < num_coordinates; ++i) |
| 42 | + { |
| 43 | + index_str += "9999"; |
| 44 | + if (i < num_coordinates - 1) |
| 45 | + { |
| 46 | + index_str += ";"; |
| 47 | + } |
| 48 | + } |
| 49 | + request += sources_str + index_str + "&" + destinations_str + index_str + "&"; |
| 50 | + |
| 51 | + // 4. Approaches: "unrestricted" has 12 chars |
| 52 | + request += "approaches="; |
| 53 | + for (unsigned i = 0; i < num_coordinates; ++i) |
| 54 | + { |
| 55 | + request += "unrestricted"; |
| 56 | + if (i < num_coordinates - 1) |
| 57 | + request += ";"; |
| 58 | + } |
| 59 | + |
| 60 | + return request; |
| 61 | +} |
| 62 | + |
| 63 | +BOOST_AUTO_TEST_SUITE(header_size) |
| 64 | + |
| 65 | +using namespace osrm; |
| 66 | +using namespace osrm::server; |
| 67 | +using namespace osrm::engine; |
| 68 | + |
| 69 | +BOOST_AUTO_TEST_CASE(minimum_header_size) |
| 70 | +{ |
| 71 | + EngineConfig config; |
| 72 | + config.max_locations_trip = 0; |
| 73 | + config.max_locations_viaroute = 0; |
| 74 | + config.max_locations_distance_table = 0; |
| 75 | + config.max_locations_map_matching = 0; |
| 76 | + |
| 77 | + unsigned result = deriveMaxHeaderSize(config); |
| 78 | + BOOST_CHECK_EQUAL(result, 8 * 1024); |
| 79 | +} |
| 80 | + |
| 81 | +BOOST_AUTO_TEST_CASE(negative_values_treated_as_zero) |
| 82 | +{ |
| 83 | + // Test with default EngineConfig values (all -1 for default) |
| 84 | + EngineConfig config; |
| 85 | + |
| 86 | + unsigned result = deriveMaxHeaderSize(config); |
| 87 | + BOOST_CHECK_EQUAL(result, 8 * 1024); |
| 88 | +} |
| 89 | + |
| 90 | +BOOST_AUTO_TEST_CASE(calculation_formula_verification) |
| 91 | +{ |
| 92 | + auto run_test_for_n_coordinates = [](unsigned num_coords) |
| 93 | + { |
| 94 | + EngineConfig config; |
| 95 | + config.max_locations_trip = num_coords; |
| 96 | + config.max_locations_viaroute = num_coords; |
| 97 | + config.max_locations_distance_table = num_coords; |
| 98 | + config.max_locations_map_matching = num_coords; |
| 99 | + |
| 100 | + unsigned calculated_size = deriveMaxHeaderSize(config); |
| 101 | + std::string worst_case_request = generateWorstCaseRequest(num_coords); |
| 102 | + |
| 103 | + BOOST_CHECK_GT(calculated_size, worst_case_request.length()); |
| 104 | + }; |
| 105 | + |
| 106 | + run_test_for_n_coordinates(10); |
| 107 | + run_test_for_n_coordinates(100); |
| 108 | + run_test_for_n_coordinates(1000); |
| 109 | + run_test_for_n_coordinates(10000); |
| 110 | +} |
| 111 | + |
| 112 | +BOOST_AUTO_TEST_SUITE_END() |
0 commit comments