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

Last change on this file since 297b796 was 6f7aff3, checked in by Michael Brooks <mlbrooks@…>, 3 years ago

String hybrid assignment to unshared now optimizes to overwrite instead of copy.

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