|
| 1 | +// Copyright (c) 2017-2025, University of Cincinnati, developed by Henry Schreiner |
| 2 | +// under NSF AWARD 1414736 and by the respective contributors. |
| 3 | +// All rights reserved. |
| 4 | +// |
| 5 | +// SPDX-License-Identifier: BSD-3-Clause |
| 6 | + |
| 7 | +#include "app_helper.hpp" |
| 8 | + |
| 9 | +#include <complex> |
| 10 | +#include <cstdint> |
| 11 | +#include <string> |
| 12 | +#include <utility> |
| 13 | +#include <vector> |
| 14 | +#include <iostream> |
| 15 | +#include <locale> |
| 16 | +#include <iomanip> |
| 17 | +#include <numeric> |
| 18 | + |
| 19 | + |
| 20 | +// Custom facet for thousands separator |
| 21 | +class CustomThousandsSeparator : public std::numpunct<char> { |
| 22 | +protected: |
| 23 | + char do_thousands_sep() const override { return '|'; } // Space separator |
| 24 | + std::string do_grouping() const override { return "\2"; } // Group digits in sets of 2 |
| 25 | +}; |
| 26 | + |
| 27 | +//derived from https://github.com/CLIUtils/CLI11/pull/1160 |
| 28 | +TEST_CASE_METHOD(TApp, "locale", "[separators]") { |
| 29 | + std::locale customLocale(std::locale::classic(), new CustomThousandsSeparator); |
| 30 | + std::locale::global(customLocale); // Set as the default system-wide locale |
| 31 | + |
| 32 | + // Ensure standard streams use the custom locale automatically |
| 33 | + std::cout.imbue(std::locale()); |
| 34 | + std::int64_t foo; |
| 35 | + std::uint64_t bar; |
| 36 | + float qux; |
| 37 | + |
| 38 | + app.add_option("FOO", foo, "Foo option") |
| 39 | + ->default_val(1234567)->force_callback(); |
| 40 | + app.add_option("BAR", bar, "Bar option") |
| 41 | + ->default_val(2345678)->force_callback(); |
| 42 | + app.add_option("QUX", qux, "QUX option") |
| 43 | + ->default_val(3456.78)->force_callback(); |
| 44 | + |
| 45 | + CHECK_NOTHROW(run()); |
| 46 | + CHECK(foo==1234567); |
| 47 | + CHECK(bar==2345678); |
| 48 | + CHECK_THAT(qux, Catch::Matchers::WithinAbs(13456.78, 0.01)); |
| 49 | +} |
0 commit comments