1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
//
// Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
// Copyright (c) 2022 Alexander Grund
//
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include "../src/shared/ios_prop.hpp"
#include "boostLocale/test/unit_test.hpp"
#include <locale>
#include <sstream>
int counter = 0;
int imbued = 0;
struct test_property {
explicit test_property(int xx = -1) : x(xx) { counter++; }
test_property(const test_property& other)
{
counter++;
x = other.x;
}
test_property& operator=(const test_property&) = default;
~test_property() { counter--; }
void on_imbue() { imbued++; }
int x;
};
typedef boost::locale::impl::ios_prop<test_property> prop_type;
struct init {
init() { prop_type::global_init(); }
};
void test_main(int /*argc*/, char** /*argv*/)
{
// Test get() default constructs the property if it does not exist or returns the existing one
{
std::stringstream ss;
TEST_EQ(prop_type::get(ss).x, -1);
TEST_EQ(counter, 1);
prop_type::get(ss).x = 42;
TEST_EQ(prop_type::get(ss).x, 42);
TEST_EQ(counter, 1);
}
TEST_EQ(counter, 0);
{
std::stringstream ss;
prop_type::get(ss).x = 1;
{
std::stringstream ss2;
TEST_EQ(counter, 1);
ss2.copyfmt(ss);
TEST_EQ(counter, 2);
TEST_EQ(prop_type::get(ss).x, 1);
TEST_EQ(prop_type::get(ss2).x, 1);
// Check that both are distinct copies
prop_type::get(ss2).x = 2;
TEST_EQ(prop_type::get(ss).x, 1);
TEST_EQ(prop_type::get(ss2).x, 2); // Only 2nd is changed
// Imbue on 2nd calls on_imbue once
TEST_EQ(imbued, 0);
ss2.imbue(std::locale::classic());
TEST_EQ(imbued, 1);
// Copy again over existing
ss2.copyfmt(ss);
TEST_EQ(counter, 2);
TEST_EQ(prop_type::get(ss).x, 1);
TEST_EQ(prop_type::get(ss2).x, 1); // Copied
prop_type::get(ss2).x = 2;
TEST_EQ(prop_type::get(ss).x, 1);
TEST_EQ(prop_type::get(ss2).x, 2); // Only this changed
}
// Copy from unset removes the property
{
std::stringstream ss2;
TEST_EQ(prop_type::get(ss).x, 1);
TEST_EQ(counter, 1);
ss.copyfmt(ss2);
TEST_EQ(counter, 0);
// All are default constructed, distinct instances
TEST_EQ(prop_type::get(ss).x, -1);
TEST_EQ(prop_type::get(ss2).x, -1);
prop_type::get(ss2).x = 2;
TEST_EQ(prop_type::get(ss).x, -1);
TEST_EQ(prop_type::get(ss2).x, 2);
TEST_EQ(counter, 2);
}
}
TEST_EQ(counter, 0);
}
|