Ejemplo n.º 1
0
int main()
{
    std::set_new_handler(my_new_handler);
#ifndef TEST_HAS_NO_EXCEPTIONS
    try
#endif
    {
        void* vp = operator new [] (std::numeric_limits<std::size_t>::max(), std::nothrow);
        DoNotOptimize(vp);
        assert(new_handler_called == 1);
        assert(vp == 0);
    }
#ifndef TEST_HAS_NO_EXCEPTIONS
    catch (...)
    {
        assert(false);
    }
#endif
    A* ap = new(std::nothrow) A[3];
    DoNotOptimize(ap);
    assert(ap);
    assert(A_constructed == 3);
    delete [] ap;
    DoNotOptimize(ap);
    assert(A_constructed == 0);
}
Ejemplo n.º 2
0
int main()
{
    A *ap = new A;
    DoNotOptimize(ap);
    assert(ap);
    assert(A_constructed);
    assert(new_called);
    delete ap;
    DoNotOptimize(ap);
    assert(!A_constructed);
    assert(!new_called);
}
Ejemplo n.º 3
0
void test_aligned() {
  typedef AlignedType<Align> T;
  T::constructed = 0;
  globalMemCounter.reset();
  std::allocator<T> a;
  const bool IsOverAlignedType = Align > MaxAligned;
  const bool ExpectAligned = IsOverAlignedType && UsingAlignedNew;
  {
    assert(globalMemCounter.checkOutstandingNewEq(0));
    assert(T::constructed == 0);
    globalMemCounter.last_new_size = 0;
    globalMemCounter.last_new_align = 0;
    T* ap = a.allocate(3);
    DoNotOptimize(ap);
    assert(globalMemCounter.checkOutstandingNewEq(1));
    assert(globalMemCounter.checkNewCalledEq(1));
    assert(globalMemCounter.checkAlignedNewCalledEq(ExpectAligned));
    assert(globalMemCounter.checkLastNewSizeEq(3 * sizeof(T)));
    assert(globalMemCounter.checkLastNewAlignEq(ExpectAligned ? Align : 0));
    assert(T::constructed == 0);
    globalMemCounter.last_delete_align = 0;
    a.deallocate(ap, 3);
    assert(globalMemCounter.checkOutstandingNewEq(0));
    assert(globalMemCounter.checkDeleteCalledEq(1));
    assert(globalMemCounter.checkAlignedDeleteCalledEq(ExpectAligned));
    assert(globalMemCounter.checkLastDeleteAlignEq(ExpectAligned ? Align : 0));
    assert(T::constructed == 0);
  }
  globalMemCounter.reset();
  {
    globalMemCounter.last_new_size = 0;
    globalMemCounter.last_new_align = 0;
    T* volatile ap2 = a.allocate(11, (const void*)5);
    DoNotOptimize(ap2);
    assert(globalMemCounter.checkOutstandingNewEq(1));
    assert(globalMemCounter.checkNewCalledEq(1));
    assert(globalMemCounter.checkAlignedNewCalledEq(ExpectAligned));
    assert(globalMemCounter.checkLastNewSizeEq(11 * sizeof(T)));
    assert(globalMemCounter.checkLastNewAlignEq(ExpectAligned ? Align : 0));
    assert(T::constructed == 0);
    globalMemCounter.last_delete_align = 0;
    a.deallocate(ap2, 11);
    DoNotOptimize(ap2);
    assert(globalMemCounter.checkOutstandingNewEq(0));
    assert(globalMemCounter.checkDeleteCalledEq(1));
    assert(globalMemCounter.checkAlignedDeleteCalledEq(ExpectAligned));
    assert(globalMemCounter.checkLastDeleteAlignEq(ExpectAligned ? Align : 0));
    assert(T::constructed == 0);
  }
}
int main(int, char**)
{
    A *ap = new (std::nothrow) A[3];
    DoNotOptimize(ap);
    assert(ap);
    assert(A_constructed == 3);
    assert(new_called);
    delete [] ap;
    DoNotOptimize(ap);
    assert(A_constructed == 0);
    assert(!new_called);

  return 0;
}
Ejemplo n.º 5
0
int main()
{
    int *x = new int(42);
    DoNotOptimize(x);
    assert(0 == unsized_delete_called);
    assert(0 == unsized_delete_nothrow_called);
    assert(0 == sized_delete_called);

    delete x;
    DoNotOptimize(x);
    assert(0 == unsized_delete_called);
    assert(1 == sized_delete_called);
    assert(0 == unsized_delete_nothrow_called);
}
Ejemplo n.º 6
0
void BM_typeSizeIter(int n, int k, std::int64_t size)
{
    auto first = Iter(n, k);
    auto last = Iter(size);
    for (; first != last; ++first)
    {
        DoNotOptimize(*first);
    }
}
Ejemplo n.º 7
0
inline void BM_PermutationsRandom(int n, int numtimes)
{
    auto X = discreture::permutations(n);

    for (int i = 0; i < numtimes; ++i)
    {
        DoNotOptimize(X.random());
    }
}
Ejemplo n.º 8
0
inline void BM_CombinationsNAP(int n, int k)
{
    discreture::Combinations<int>::combination comb(k);
    discreture::Combinations<int>::size_type hint = k;
    discreture::Combinations<int>::combination::value_type last = k - 1;
    std::iota(comb.begin(), comb.end(), 0);
    do
    {
        DoNotOptimize(comb);
    } while (discreture::Combinations<int>::next_combination(n, comb, hint, last));
}
Ejemplo n.º 9
0
inline void BM_combinationsIterator(int n, int k, std::int64_t size)
{
    UNUSED(size);
    auto X = discreture::combinations(n, k);
    auto first = X.begin();
    auto last = X.end();
    for (; first != last; ++first)
    {
        DoNotOptimize(*first);
    }
}
Ejemplo n.º 10
0
void do_convert_type (const std::vector<S> &svec, std::vector<D> &dvec)
{
    convert_type (&svec[0], &dvec[0], svec.size());
    DoNotOptimize (dvec[0]);  // Be sure nothing is optimized away
}