summaryrefslogtreecommitdiff
path: root/test/unit/memory.h
blob: 44eec00d08a3d0e2fcd2c7cb4e06bd93f43ef8ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*********************                                                        */
/*! \file memory.h
 ** \verbatim
 ** Original author: Morgan Deters
 ** Major contributors: none
 ** Minor contributors (to current version): none
 ** This file is part of the CVC4 project.
 ** Copyright (c) 2009-2013  New York University and The University of Iowa
 ** See the file COPYING in the top-level source directory for licensing
 ** information.\endverbatim
 **
 ** \brief Utility class to help testing out-of-memory conditions.
 **
 ** Utility class to help testing out-of-memory conditions.
 **
 ** Use it like this (for example):
 **
 **   CVC4::test::WithLimitedMemory wlm(amount);
 **   TS_ASSERT_THROWS( foo(), bad_alloc );
 **
 ** The WithLimitedMemory destructor will re-establish the previous limit.
 **/

#include <cxxtest/TestSuite.h>

#ifndef __CVC4__TEST__MEMORY_H
#define __CVC4__TEST__MEMORY_H

#include <sys/time.h>
#include <sys/resource.h>

#include "util/cvc4_assert.h"

namespace CVC4 {
namespace test {

class WithLimitedMemory {
  rlim_t d_prevAmount;

  void remember() {
    struct rlimit rlim;
    TS_ASSERT_EQUALS(getrlimit(RLIMIT_AS, &rlim), 0);
    d_prevAmount = rlim.rlim_cur;
  }

public:

  WithLimitedMemory() {
#ifdef __APPLE__
    TS_FAIL("setrlimit() is broken on Mac, can't run memory tests.");
    AlwaysAssert(false,
                 "setrlimit() is broken on Mac, can't run memory tests.");
#endif /* __APPLE__ */
    remember();
  }

  WithLimitedMemory(rlim_t amount) {
#ifdef __APPLE__
    TS_FAIL("setrlimit() is broken on Mac, can't run memory tests.");
    AlwaysAssert(false,
                 "setrlimit() is broken on Mac, can't run memory tests.");
#endif /* __APPLE__ */
    remember();
    set(amount);
  }

  ~WithLimitedMemory() {
    set(d_prevAmount);
  }

  void set(rlim_t amount) {
    struct rlimit rlim;
    rlim.rlim_cur = amount;
    rlim.rlim_max = RLIM_INFINITY;
    TS_ASSERT_EQUALS(setrlimit(RLIMIT_AS, &rlim), 0);
  }
};

}/* CVC4::test namespace */
}/* CVC4 namespace */

#endif /* __CVC4__TEST__MEMORY_H */
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback