Lumiera  0.pre.03
»edit your freedom«
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) Lumiera.org
5  2014, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
28 #include "lib/test/run.hpp"
29 #include "lib/test/test-helper.hpp"
31 #include "lib/util.hpp"
32 
33 #include <utility>
34 #include <cstring>
35 #include <string>
36 
37 using std::string;
38 using util::isnil;
39 
40 
41 namespace lib {
42 namespace test{
43 
44  namespace {
45 
46  void*
47  mallocMess (size_t siz)
48  {
49  return malloc(siz);
50  }
51  }
52 
53 
54 
55 
56 
57 
58 
59 
60  /***************************************************************************/
77  class UniqueMallocOwner_test : public Test
78  {
79 
81 
82  virtual void
83  run (Arg args)
84  {
85  string probeString = isnil(args)? randStr(123) : args[0];
86  auto hotPotato = place_into_malloced_buffer (probeString);
87 
88  CHECK (!isnil (hotPotato));
89  verify_and_consume (std::move(hotPotato), probeString);
90  CHECK (isnil (hotPotato));
91  }
92 
93 
94  CharOwner
95  place_into_malloced_buffer (string probeString)
96  {
97  CharOwner hotPotato(mallocMess (1 + probeString.length()));
98  std::strcpy(hotPotato.get(), probeString.c_str());
99  return hotPotato;
100  }
101 
102 
103  void
104  verify_and_consume (CharOwner hotPotato, string refString)
105  {
106  CHECK (refString == hotPotato.get());
107 
108  } // note sideeffect: hotPotato goes out of scope here...
109  };
110 
111 
113  LAUNCHER (UniqueMallocOwner_test, "unit common");
114 
115 
116 
117 }} // namespace lib::test
Definition: run.hpp:49
Helper to deal with C-MALLOCed memory automatically.
Implementation namespace for support and library code.
string randStr(size_t len)
create garbage string of given length
Definition: test-helper.cpp:69
Simple test class runner.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
A collection of frequently used helper functions to support unit testing.
Ownership token for a piece of heap memory allocated in plain-C style.