1 /* ///////////////////////////////////////////////////////////////////////
2 * File: shared_counter_test.h
10 * Copyright (c) 2008-2020, Waruqi All rights reserved.
11 * //////////////////////////////////////////////////////////////////// */
12 #ifndef EXTL_SHARED_COUNTER_TEST_H
13 #define EXTL_SHARED_COUNTER_TEST_H
15 #ifndef EXTL_SHARED_COUNTER_H
16 # error This file must be included of shared_counter.h
19 /* ///////////////////////////////////////////////////////////////////////
20 * ::unit_test namespace
22 EXTL_TEST_BEGIN_NAMESPACE
24 /* ///////////////////////////////////////////////////////////////////////
27 struct shared_counter_test
29 struct resource_deleter
31 void operator()(e_int_t
* res
)
33 if(NULL
!= res
) *res
= 0;
39 typedef res_owner class_type
;
43 : resource(NULL
), sc()
46 res_owner(e_int_t
* res
)
47 : resource(res
), sc(res
)
50 #ifdef EXTL_TEMPLATE_CLASS_DEFAULT_ARGUMENT_SUPPORT
51 typedef shared_counter
<e_int_t
, resource_deleter
> shared_counter_type
;
53 typedef shared_counter
< e_int_t
55 , synch_traits_selector::synch_traits_type
56 , allocator_selector
< e_byte_t
>::allocator_type
57 > shared_counter_type
;
62 shared_counter_type sc
;
67 res_owner
sct1(new e_int_t(10)), sct2
;
68 EXTL_ASSERT(sct1
.sc
.count() == 1);
69 EXTL_ASSERT(sct2
.sc
.count() == 0);
72 EXTL_ASSERT(sct1
.sc
.count() == 2);
73 EXTL_ASSERT(sct2
.sc
.count() == 2);
74 EXTL_ASSERT(*sct1
.resource
== 10);
75 EXTL_ASSERT(*sct2
.resource
== 10);
78 EXTL_ASSERT(sct1
.sc
.count() == 2);
79 EXTL_ASSERT(sct2
.sc
.count() == 2);
80 EXTL_ASSERT(*sct1
.resource
== 10);
81 EXTL_ASSERT(*sct2
.resource
== 10);
83 sct1
.sc
.swap(sct2
.sc
);
84 EXTL_ASSERT(sct1
.sc
.count() == 2);
85 EXTL_ASSERT(sct2
.sc
.count() == 2);
86 EXTL_ASSERT(*sct1
.resource
== 10);
87 EXTL_ASSERT(*sct2
.resource
== 10);
91 res_owner
sct3(new e_int_t(15));
92 EXTL_ASSERT(*sct3
.resource
== 15);
96 EXTL_ASSERT(*pRes
== 0);
102 shared_counter_test g_shared_counter_test
;
104 /* ///////////////////////////////////////////////////////////////////////
105 * ::unit_test namespace
107 EXTL_TEST_END_NAMESPACE
109 /* //////////////////////////////////////////////////////////////////// */
110 #endif /* EXTL_SHARED_COUNTER_TEST_H */
111 /* //////////////////////////////////////////////////////////////////// */