Lumiera 0.pre.04
»edit your freedom«
Loading...
Searching...
No Matches
unique-malloc-owner-test.cpp
Go to the documentation of this file.
1/*
2 UniqueMallocOwner(Test) - Concept to dispatch according to the verbs of a DSL
3
4 Copyright (C)
5 2014, Hermann Vosseler <Ichthyostega@web.de>
6
7  **Lumiera** is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License as published by the
9  Free Software Foundation; either version 2 of the License, or (at your
10  option) any later version. See the file COPYING for further details.
11
12* *****************************************************************/
13
19#include "lib/test/run.hpp"
22#include "lib/util.hpp"
23
24#include <utility>
25#include <cstring>
26#include <string>
27
28using std::string;
29using util::isnil;
30
31
32namespace lib {
33namespace test{
34
35 namespace {
36
37 void*
38 mallocMess (size_t siz)
39 {
40 return malloc(siz);
41 }
42 }
43
44
45
46
47
48
49
50
51 /***************************************************************************/
68 class UniqueMallocOwner_test : public Test
69 {
70
72
73 virtual void
74 run (Arg args)
75 {
76 string probeString = isnil(args)? randStr(123) : args[0];
77 auto hotPotato = place_into_malloced_buffer (probeString);
78
79 CHECK (!isnil (hotPotato));
80 verify_and_consume (std::move(hotPotato), probeString);
81 CHECK (isnil (hotPotato));
82 }
83
84
86 place_into_malloced_buffer (string probeString)
87 {
88 CharOwner hotPotato(mallocMess (1 + probeString.length()));
89 std::strcpy(hotPotato.get(), probeString.c_str());
90 return hotPotato;
91 }
92
93
94 void
95 verify_and_consume (CharOwner hotPotato, string refString)
96 {
97 CHECK (refString == hotPotato.get());
98
99 } // note sideeffect: hotPotato goes out of scope here...
100 };
101
102
105
106
107
108}} // namespace lib::test
Ownership token for a piece of heap memory allocated in plain-C style.
void verify_and_consume(CharOwner hotPotato, string refString)
CharOwner place_into_malloced_buffer(string probeString)
string randStr(size_t len)
create garbage string of given length
Implementation namespace for support and library code.
Test runner and basic definitions for tests.
bool isnil(lib::time::Duration const &dur)
Simplistic test class runner.
#define LAUNCHER(_TEST_CLASS_, _GROUPS_)
Definition run.hpp:116
A collection of frequently used helper functions to support unit testing.
Helper to deal with C-MALLOCed memory automatically.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...