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)
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"
20 #include "lib/test/test-helper.hpp"
22 #include "lib/util.hpp"
23 
24 #include <utility>
25 #include <cstring>
26 #include <string>
27 
28 using std::string;
29 using util::isnil;
30 
31 
32 namespace lib {
33 namespace 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 
85  CharOwner
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 
104  LAUNCHER (UniqueMallocOwner_test, "unit common");
105 
106 
107 
108 }} // namespace lib::test
Definition: run.hpp:40
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:61
Simplistic 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.