~ubuntu-branches/ubuntu/saucy/libc++/saucy-proposed

« back to all changes in this revision

Viewing changes to libcxx/test/language.support/support.dynamic/new.delete/new.delete.single/new_nothrow_replace.pass.cpp

  • Committer: Package Import Robot
  • Author(s): Andrej Belym
  • Date: 2012-06-02 19:25:47 UTC
  • Revision ID: package-import@ubuntu.com-20120602192547-b6ta950a8ckc9um2
Tags: upstream-1.0~svn160132
ImportĀ upstreamĀ versionĀ 1.0~svn160132

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//===----------------------------------------------------------------------===//
 
2
//
 
3
//                     The LLVM Compiler Infrastructure
 
4
//
 
5
// This file is dual licensed under the MIT and the University of Illinois Open
 
6
// Source Licenses. See LICENSE.TXT for details.
 
7
//
 
8
//===----------------------------------------------------------------------===//
 
9
 
 
10
 // test operator new nothrow by replacing only operator new
 
11
 
 
12
#include <new>
 
13
#include <cstddef>
 
14
#include <cstdlib>
 
15
#include <cassert>
 
16
#include <limits>
 
17
 
 
18
int new_called = 0;
 
19
 
 
20
void* operator new(std::size_t s) throw(std::bad_alloc)
 
21
{
 
22
    ++new_called;
 
23
    return std::malloc(s);
 
24
}
 
25
 
 
26
void  operator delete(void* p) throw()
 
27
{
 
28
    --new_called;
 
29
    std::free(p);
 
30
}
 
31
 
 
32
bool A_constructed = false;
 
33
 
 
34
struct A
 
35
{
 
36
    A() {A_constructed = true;}
 
37
    ~A() {A_constructed = false;}
 
38
};
 
39
 
 
40
int main()
 
41
{
 
42
    A* ap = new (std::nothrow) A;
 
43
    assert(ap);
 
44
    assert(A_constructed);
 
45
    assert(new_called);
 
46
    delete ap;
 
47
    assert(!A_constructed);
 
48
    assert(!new_called);
 
49
}