MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / copyOnWrite

Method copyOnWrite

test/testprogrammemory.cpp:41–87  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

39 }
40
41 void copyOnWrite() const {
42 SimpleTokenList tokenlist("1+1;");
43 Token* tok = tokenlist.front();
44 const nonneg int id = 123;
45 tok->exprId(id);
46
47 ProgramMemory pm;
48 const ValueFlow::Value* v = pm.getValue(id);
49 ASSERT(!v);
50 pm.setValue(tok, ValueFlow::Value{41});
51
52 v = pm.getValue(id);
53 ASSERT(v);
54 ASSERT_EQUALS(41, v->intvalue);
55
56 // create a copy
57 ProgramMemory pm2 = pm;
58
59 // make sure the value was copied
60 v = pm2.getValue(id);
61 ASSERT(v);
62 ASSERT_EQUALS(41, v->intvalue);
63
64 // set a value in the copy to trigger copy-on-write
65 pm2.setValue(tok, ValueFlow::Value{42});
66
67 // make another copy and set another value
68 ProgramMemory pm3 = pm2;
69
70 // set a value in the copy to trigger copy-on-write
71 pm3.setValue(tok, ValueFlow::Value{43});
72
73 // make sure the value was set
74 v = pm2.getValue(id);
75 ASSERT(v);
76 ASSERT_EQUALS(42, v->intvalue);
77
78 // make sure the value was set
79 v = pm3.getValue(id);
80 ASSERT(v);
81 ASSERT_EQUALS(43, v->intvalue);
82
83 // make sure the original value remains unchanged
84 v = pm.getValue(id);
85 ASSERT(v);
86 ASSERT_EQUALS(41, v->intvalue);
87 }
88
89 void hasValue() const {
90 ProgramMemory pm;

Callers

nothing calls this directly

Calls 3

frontMethod · 0.80
getValueMethod · 0.45
setValueMethod · 0.45

Tested by

no test coverage detected