1 | #include <string.hfa>
|
---|
2 | #include <string_sharectx.hfa>
|
---|
3 | #include <string_res.hfa>
|
---|
4 | #include <fstream.hfa>
|
---|
5 |
|
---|
6 | // In these tests, shared heaps are never remotely full and string sizes are tiny.
|
---|
7 | // So here, the SUT should put a yes-sharing string in a heap with lots of spare room.
|
---|
8 | // The SUT should always keep a no-sharing string's buffer 1x--2x the string's size.
|
---|
9 | // This check uses 3x as a heuristic split between those cases.
|
---|
10 | void assertSpareRoomInHeap( string & s, bool expectOversized ) {
|
---|
11 | double bytesInHeap = DEBUG_string_bytes_in_heap(s.inner->Handle.ulink);
|
---|
12 | double bytesUsed = s.inner->Handle.lnth;
|
---|
13 | double overhead = bytesInHeap / bytesUsed;
|
---|
14 | assert (overhead >= 1);
|
---|
15 | if ( expectOversized )
|
---|
16 | assert( overhead >= 3.0 );
|
---|
17 | else
|
---|
18 | assert( overhead < 3.0 );
|
---|
19 | }
|
---|
20 |
|
---|
21 | void baseline() {
|
---|
22 | string x = "hi";
|
---|
23 | assertSpareRoomInHeap( x, true );
|
---|
24 |
|
---|
25 | string y = x; // construct y in same context, no write yet => no copy yet
|
---|
26 | assertSpareRoomInHeap( y, true );
|
---|
27 | assert( y.inner->Handle.s == x.inner->Handle.s);
|
---|
28 | sout | y; // hi
|
---|
29 |
|
---|
30 | x = "bye";
|
---|
31 | assertSpareRoomInHeap( x, true );
|
---|
32 | y = x; // y in same context, no write yet => no copy yet
|
---|
33 | assertSpareRoomInHeap( y, true );
|
---|
34 | assert( y.inner->Handle.s == x.inner->Handle.s);
|
---|
35 | sout | y; // bye
|
---|
36 | }
|
---|
37 |
|
---|
38 | void eagerCopy() {
|
---|
39 | string x = "hi";
|
---|
40 | assertSpareRoomInHeap( x, true );
|
---|
41 | string_sharectx c = { NEW_SHARING };
|
---|
42 |
|
---|
43 | string y = x; // construct y in different context => eager copy
|
---|
44 | assertSpareRoomInHeap( y, true );
|
---|
45 | assert( y.inner->Handle.s != x.inner->Handle.s);
|
---|
46 | sout | y; // hi
|
---|
47 |
|
---|
48 | x = "bye";
|
---|
49 | assertSpareRoomInHeap( x, true );
|
---|
50 | y = x; // y was already in different context => eager copy
|
---|
51 | assertSpareRoomInHeap( y, true );
|
---|
52 | assert( y.inner->Handle.s != x.inner->Handle.s);
|
---|
53 | sout | y; // bye
|
---|
54 | }
|
---|
55 |
|
---|
56 | void soloAlloc() {
|
---|
57 | string x = "hi";
|
---|
58 | assertSpareRoomInHeap( x, true );
|
---|
59 | string_sharectx c = { NO_SHARING };
|
---|
60 |
|
---|
61 | string y = x; // y allocates into private pad, implying eager copy
|
---|
62 | assertSpareRoomInHeap( y, false );
|
---|
63 | assert( y.inner->Handle.s != x.inner->Handle.s);
|
---|
64 | sout | y; // hi
|
---|
65 |
|
---|
66 | x = "bye";
|
---|
67 | assertSpareRoomInHeap( x, true );
|
---|
68 | y = x; // into private y => eager copy
|
---|
69 | assertSpareRoomInHeap( y, false );
|
---|
70 | assert( y.inner->Handle.s != x.inner->Handle.s);
|
---|
71 | sout | y; // bye
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 | int main() {
|
---|
76 | baseline();
|
---|
77 | eagerCopy();
|
---|
78 | soloAlloc();
|
---|
79 | printf("done\n");
|
---|
80 | }
|
---|