source: tests/collections/string-ctx-manage.cfa@ b28ce93

Last change on this file since b28ce93 was 3f631d6, checked in by Michael Brooks <mlbrooks@…>, 6 months ago

Switch string io to be generic upon iostream, rather than specific upon fstream.

No direct test added because we currently lack a second implementation of the abstract streams.

Benefit is removing polymorphism-induced noise from cost calculations, in upcoming string-overload reorganizations.

The change in tests 'collections/string-operator*' shows cases where we stopped picking string for the wrong reason. (If we should be picking string for a better reason, that's just ahead.)

  • Property mode set to 100644
File size: 2.4 KB
Line 
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.
10void 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
21void 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
38void 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
56void 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
75int main() {
76 baseline();
77 eagerCopy();
78 soloAlloc();
79 printf("done\n");
80}
Note: See TracBrowser for help on using the repository browser.