|
| 1 | +#include <iostream> |
| 2 | +#include <string> |
| 3 | +#include <vector> |
| 4 | + |
| 5 | +#include "absl/strings/str_cat.h" |
| 6 | +#include "absl/strings/str_split.h" |
| 7 | +#include "absl/strings/string_view.h" |
| 8 | +#include "absl/strings/str_format.h" |
| 9 | +#include "absl/container/flat_hash_map.h" |
| 10 | +#include "absl/container/flat_hash_set.h" |
| 11 | +#include "absl/status/status.h" |
| 12 | +#include "absl/status/statusor.h" |
| 13 | +#include "absl/time/time.h" |
| 14 | +#include "absl/time/clock.h" |
| 15 | + |
| 16 | +// Report helper |
| 17 | +static int failures = 0; |
| 18 | +#define CHECK(cond) \ |
| 19 | + do { \ |
| 20 | + if (!(cond)) { \ |
| 21 | + std::cerr << "FAIL: " #cond " (line " << __LINE__ << ")" << std::endl; \ |
| 22 | + ++failures; \ |
| 23 | + } \ |
| 24 | + } while (false) |
| 25 | + |
| 26 | +// --------------------------------------------------------------------------- |
| 27 | +// Test absl::strings |
| 28 | +// --------------------------------------------------------------------------- |
| 29 | +void test_strings() { |
| 30 | + std::cout << "Testing absl::strings..." << std::endl; |
| 31 | + |
| 32 | + // StrCat |
| 33 | + std::string s = absl::StrCat("Hello", ", ", "World", "!"); |
| 34 | + CHECK(s == "Hello, World!"); |
| 35 | + |
| 36 | + // StrFormat |
| 37 | + std::string formatted = absl::StrFormat("value=%d pi=%.2f", 42, 3.14159); |
| 38 | + CHECK(formatted == "value=42 pi=3.14"); |
| 39 | + |
| 40 | + // StrSplit |
| 41 | + std::vector<std::string> parts = absl::StrSplit("a,b,c,d", ','); |
| 42 | + CHECK(parts.size() == 4); |
| 43 | + CHECK(parts[0] == "a"); |
| 44 | + CHECK(parts[3] == "d"); |
| 45 | + |
| 46 | + // string_view |
| 47 | + absl::string_view sv = "abcdef"; |
| 48 | + CHECK(sv.size() == 6); |
| 49 | + CHECK(sv.substr(1, 3) == "bcd"); |
| 50 | + |
| 51 | + std::cout << " absl::strings OK" << std::endl; |
| 52 | +} |
| 53 | + |
| 54 | +// --------------------------------------------------------------------------- |
| 55 | +// Test absl::flat_hash_map / flat_hash_set |
| 56 | +// --------------------------------------------------------------------------- |
| 57 | +void test_containers() { |
| 58 | + std::cout << "Testing absl::containers..." << std::endl; |
| 59 | + |
| 60 | + // flat_hash_map |
| 61 | + absl::flat_hash_map<std::string, int> map; |
| 62 | + map["one"] = 1; |
| 63 | + map["two"] = 2; |
| 64 | + map["three"] = 3; |
| 65 | + CHECK(map.size() == 3); |
| 66 | + CHECK(map["one"] == 1); |
| 67 | + CHECK(map.contains("two")); |
| 68 | + CHECK(!map.contains("four")); |
| 69 | + |
| 70 | + // flat_hash_set |
| 71 | + absl::flat_hash_set<int> set = {1, 2, 3, 4, 5}; |
| 72 | + CHECK(set.size() == 5); |
| 73 | + CHECK(set.contains(3)); |
| 74 | + CHECK(!set.contains(6)); |
| 75 | + set.insert(6); |
| 76 | + CHECK(set.contains(6)); |
| 77 | + |
| 78 | + std::cout << " absl::containers OK" << std::endl; |
| 79 | +} |
| 80 | + |
| 81 | +// --------------------------------------------------------------------------- |
| 82 | +// Test absl::Status / absl::StatusOr |
| 83 | +// --------------------------------------------------------------------------- |
| 84 | +absl::StatusOr<int> safe_divide(int a, int b) { |
| 85 | + if (b == 0) { |
| 86 | + return absl::InvalidArgumentError("division by zero"); |
| 87 | + } |
| 88 | + return a / b; |
| 89 | +} |
| 90 | + |
| 91 | +void test_status() { |
| 92 | + std::cout << "Testing absl::Status / absl::StatusOr..." << std::endl; |
| 93 | + |
| 94 | + // OK status |
| 95 | + absl::Status ok = absl::OkStatus(); |
| 96 | + CHECK(ok.ok()); |
| 97 | + |
| 98 | + // Error status |
| 99 | + absl::Status err = absl::NotFoundError("item not found"); |
| 100 | + CHECK(!err.ok()); |
| 101 | + CHECK(err.code() == absl::StatusCode::kNotFound); |
| 102 | + |
| 103 | + // StatusOr success |
| 104 | + auto result = safe_divide(10, 2); |
| 105 | + CHECK(result.ok()); |
| 106 | + CHECK(*result == 5); |
| 107 | + |
| 108 | + // StatusOr failure |
| 109 | + auto bad = safe_divide(10, 0); |
| 110 | + CHECK(!bad.ok()); |
| 111 | + CHECK(bad.status().code() == absl::StatusCode::kInvalidArgument); |
| 112 | + |
| 113 | + std::cout << " absl::Status OK" << std::endl; |
| 114 | +} |
| 115 | + |
| 116 | +// --------------------------------------------------------------------------- |
| 117 | +// Test absl::Time |
| 118 | +// --------------------------------------------------------------------------- |
| 119 | +void test_time() { |
| 120 | + std::cout << "Testing absl::Time..." << std::endl; |
| 121 | + |
| 122 | + absl::Time epoch = absl::UnixEpoch(); |
| 123 | + absl::Time now = absl::Now(); |
| 124 | + |
| 125 | + // now should be strictly after epoch |
| 126 | + CHECK(now > epoch); |
| 127 | + |
| 128 | + // Duration arithmetic |
| 129 | + absl::Duration one_hour = absl::Hours(1); |
| 130 | + absl::Duration one_min = absl::Minutes(1); |
| 131 | + CHECK(one_hour == 60 * one_min); |
| 132 | + |
| 133 | + absl::Time future = epoch + absl::Seconds(3600); |
| 134 | + CHECK(future - epoch == one_hour); |
| 135 | + |
| 136 | + std::cout << " absl::Time OK" << std::endl; |
| 137 | +} |
| 138 | + |
| 139 | +// --------------------------------------------------------------------------- |
| 140 | +// Main |
| 141 | +// --------------------------------------------------------------------------- |
| 142 | +int main() { |
| 143 | + std::cout << "=== Testing Abseil C++ library ===" << std::endl; |
| 144 | + |
| 145 | + test_strings(); |
| 146 | + test_containers(); |
| 147 | + test_status(); |
| 148 | + test_time(); |
| 149 | + |
| 150 | + if (failures > 0) { |
| 151 | + std::cerr << failures << " test(s) FAILED." << std::endl; |
| 152 | + return 1; |
| 153 | + } |
| 154 | + |
| 155 | + std::cout << "=== All Abseil tests passed! ===" << std::endl; |
| 156 | + return 0; |
| 157 | +} |
0 commit comments