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

Last change on this file since 1a40870 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
RevLine 
[0f781fb8]1#include <string.hfa>
2#include <string_sharectx.hfa>
[4b3b352]3#include <string_res.hfa>
[3f631d6]4#include <fstream.hfa>
[0f781fb8]5
[6f7aff3]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
[4b3b352]21void baseline() {
22 string x = "hi";
[6f7aff3]23 assertSpareRoomInHeap( x, true );
[4b3b352]24
25 string y = x; // construct y in same context, no write yet => no copy yet
[6f7aff3]26 assertSpareRoomInHeap( y, true );
[4b3b352]27 assert( y.inner->Handle.s == x.inner->Handle.s);
28 sout | y; // hi
29
30 x = "bye";
[6f7aff3]31 assertSpareRoomInHeap( x, true );
[4b3b352]32 y = x; // y in same context, no write yet => no copy yet
[6f7aff3]33 assertSpareRoomInHeap( y, true );
[4b3b352]34 assert( y.inner->Handle.s == x.inner->Handle.s);
35 sout | y; // bye
36}
[0f781fb8]37
[4b3b352]38void eagerCopy() {
[0f781fb8]39 string x = "hi";
[6f7aff3]40 assertSpareRoomInHeap( x, true );
[4b3b352]41 string_sharectx c = { NEW_SHARING };
[0f781fb8]42
[4b3b352]43 string y = x; // construct y in different context => eager copy
[6f7aff3]44 assertSpareRoomInHeap( y, true );
[4b3b352]45 assert( y.inner->Handle.s != x.inner->Handle.s);
46 sout | y; // hi
47
[804bf677]48 x = "bye";
[6f7aff3]49 assertSpareRoomInHeap( x, true );
[4b3b352]50 y = x; // y was already in different context => eager copy
[6f7aff3]51 assertSpareRoomInHeap( y, true );
[4b3b352]52 assert( y.inner->Handle.s != x.inner->Handle.s);
53 sout | y; // bye
[0f781fb8]54}
55
[804bf677]56void soloAlloc() {
[0f781fb8]57 string x = "hi";
[6f7aff3]58 assertSpareRoomInHeap( x, true );
[0f781fb8]59 string_sharectx c = { NO_SHARING };
[804bf677]60
61 string y = x; // y allocates into private pad, implying eager copy
[6f7aff3]62 assertSpareRoomInHeap( y, false );
[804bf677]63 assert( y.inner->Handle.s != x.inner->Handle.s);
64 sout | y; // hi
65
[7b0e8b7]66 x = "bye";
[6f7aff3]67 assertSpareRoomInHeap( x, true );
[7b0e8b7]68 y = x; // into private y => eager copy
[6f7aff3]69 assertSpareRoomInHeap( y, false );
[7b0e8b7]70 assert( y.inner->Handle.s != x.inner->Handle.s);
71 sout | y; // bye
[0f781fb8]72}
73
74
75int main() {
[4b3b352]76 baseline();
77 eagerCopy();
[804bf677]78 soloAlloc();
[0f781fb8]79 printf("done\n");
80}
Note: See TracBrowser for help on using the repository browser.