Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete TestUtils mixin #53

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 0 additions & 33 deletions source/automem/unique.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import automem.traits: isAllocator;
import std.experimental.allocator: theAllocator;
import std.typecons: Flag;

version(AutomemTesting) {
import ut;
mixin TestUtils;
}

version (D_BetterC)
enum gcExists = false;
Expand Down Expand Up @@ -120,13 +116,6 @@ struct Unique(
return u;
}

/// release ownership
package Pointer release() {
auto ret = _object;
_object = null;
return ret;
}

///
package Allocator allocator() {
return _allocator;
Expand Down Expand Up @@ -206,28 +195,6 @@ private:
}


///
@("release")
@system unittest {
import std.experimental.allocator: dispose;
import core.exception: AssertError;

try {
auto allocator = TestAllocator();
auto ptr = Unique!(Struct, TestAllocator*)(&allocator, 42);
ptr.release;
assert(Struct.numStructs == 1);
} catch(AssertError e) { // TestAllocator should throw due to memory leak
version(unitThreadedLight) {}
else
"Memory leak in TestAllocator".should.be in e.msg;
return;
}

assert(0); // should throw above
}


private template makeObject(Flag!"supportGC" supportGC, args...)
{
void makeObject(Type,A)(ref Unique!(Type, A) u) {
Expand Down
226 changes: 1 addition & 225 deletions tests/ut/package.d
Original file line number Diff line number Diff line change
@@ -1,229 +1,5 @@
module ut;

public import unit_threaded;
public import unit_threaded.should: should; // FIXME
public import ut.utils;
public import test_allocator: TestAllocator;

mixin template TestUtils() {
import unit_threaded;
import test_allocator;

/**
Returns an object that, while in scope, replaces whatever
theAllocator was with TestAllocator.
*/
auto theTestAllocator() {
static struct Context {
import std.experimental.allocator: theAllocator;

TestAllocator testAllocator;
typeof(theAllocator) oldAllocator;

static auto create() {
import std.experimental.allocator: allocatorObject;

Context ctx;

ctx.oldAllocator = theAllocator;
theAllocator = allocatorObject(ctx.testAllocator);

return ctx;
}

~this() {
import std.experimental.allocator: dispose;

// 2.079.0 changed the API - we check here
static if(__traits(compiles, testAllocator.dispose(theAllocator)))
testAllocator.dispose(theAllocator);

theAllocator = oldAllocator;
}
}

return Context.create;
}

@Setup
void before() {
}

@Shutdown
void after() {
reset;
}

void reset() {
Struct.numStructs = 0;
Class.numClasses = 0;
SharedStruct.numStructs = 0;
NoGcStruct.numStructs = 0;
}


void _writelnUt(T...)(T args) {
try {
() @trusted { writelnUt(args); }();
} catch(Exception ex) {
assert(false);
}
}

private struct Struct {
int i;
static int numStructs = 0;

this(int i) @safe nothrow {
this.i = i;

++numStructs;
_writelnUt("Struct ", &this, " normal ctor, i=", i, ", N=", numStructs);
}

this(this) @safe nothrow {
++numStructs;
_writelnUt("Struct ", &this, " postBlit ctor, i=", i, ", N=", numStructs);
}

~this() @safe nothrow const {
--numStructs;
_writelnUt("Struct ", &this, " dtor, i=", i, ", N=", numStructs);
}

int twice() @safe pure const nothrow {
return i * 2;
}
}

private struct SharedStruct {
int i;
static int numStructs = 0;

this(int i) @safe nothrow shared {
this.i = i;

++numStructs;
try () @trusted {
_writelnUt("Struct normal ctor ", &this, ", i=", i, ", N=", numStructs);
}();
catch(Exception ex) {}
}

this(this) @safe nothrow shared {
++numStructs;
try () @trusted {
_writelnUt("Struct postBlit ctor ", &this, ", i=", i, ", N=", numStructs);
}();
catch(Exception ex) {}
}

~this() @safe nothrow {
--numStructs;
try () @trusted { _writelnUt("Struct dtor ", &this, ", i=", i, ", N=", numStructs); }();
catch(Exception ex) {}
}

int twice() @safe pure const nothrow shared {
return i * 2;
}
}

private class Class {
int i;
static int numClasses = 0;

this(int i) @safe nothrow {
this.i = i;
++numClasses;
}

~this() @safe nothrow {
--numClasses;
}

int twice() @safe pure const nothrow {
return i * 2;
}
}

private class SharedClass {
int i;
static int numClasss = 0;

this(int i) @safe nothrow shared {
this.i = i;

++numClasss;
try () @trusted {
_writelnUt("SharedClass normal ctor ", this, ", i=", i, ", N=", numClasss);
}();
catch(Exception ex) {}
}

~this() @safe nothrow {
--numClasss;
try () @trusted { _writelnUt("SharedClass dtor ", this, ", i=", i, ", N=", numClasss); }();
catch(Exception ex) {}
}

int twice() @safe pure const nothrow shared {
return i * 2;
}
}


private struct SafeAllocator {

import std.experimental.allocator.mallocator: Mallocator;

void[] allocate(this T)(size_t i) @trusted nothrow @nogc {
return Mallocator.instance.allocate(i);
}

void deallocate(this T)(void[] bytes) @trusted nothrow @nogc {
Mallocator.instance.deallocate(bytes);
}
}

private struct NoGcStruct {
int i;

static int numStructs = 0;

this(int i) @safe @nogc nothrow {
this.i = i;

++numStructs;
}

this(this) @safe @nogc nothrow {
++numStructs;
}

~this() @safe @nogc nothrow {
--numStructs;
}

}

private class NoGcClass {
int i;
static int numClasses = 0;

this(int i) @safe @nogc nothrow {
this.i = i;
++numClasses;
}

~this() @safe @nogc nothrow {
--numClasses;
}
}

private struct SharedStructWithIndirection {
string s;
this(string s) shared {
this.s = s;
}
}
}
9 changes: 8 additions & 1 deletion tests/ut/ref_counted.d
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
module ut.ref_counted;


import ut;
import automem.ref_counted;

mixin TestUtils;

@Shutdown
void after() {
reset;
}

//mixin TestUtils;

///
@("struct test allocator no copies")
Expand Down
12 changes: 8 additions & 4 deletions tests/ut/unique.d
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import ut;
import automem.unique;


mixin TestUtils;

@Shutdown
void after() {
reset;
}


///
Expand Down Expand Up @@ -87,7 +91,7 @@ mixin TestUtils;
auto allocator = TestAllocator();
auto oldPtr = Unique!(Struct, TestAllocator*)(&allocator, 5);
Unique!(Struct, TestAllocator*) newPtr = oldPtr.move;
oldPtr.shouldBeNull;
oldPtr.borrow.shouldBeNull;
newPtr.twice.shouldEqual(10);
Struct.numStructs.shouldEqual(1);
}
Expand Down Expand Up @@ -142,7 +146,7 @@ mixin TestUtils;
auto oldPtr = Unique!(Struct, TestAllocator*)(&allocator, 5);
auto newPtr = oldPtr.unique;
newPtr.twice.shouldEqual(10);
oldPtr.shouldBeNull;
oldPtr.borrow.shouldBeNull;
}

@("@nogc")
Expand Down Expand Up @@ -203,7 +207,7 @@ mixin TestUtils;
Struct.numStructs.shouldEqual(2);
ptr1 = ptr2.move;
Struct.numStructs.shouldEqual(1);
ptr2.shouldBeNull;
ptr2.borrow.shouldBeNull;
ptr1.twice.shouldEqual(20);
}

Expand Down
Loading