diff --git a/source/automem/unique.d b/source/automem/unique.d index ff37974..8a8ab0d 100644 --- a/source/automem/unique.d +++ b/source/automem/unique.d @@ -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; @@ -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; @@ -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) { diff --git a/tests/ut/package.d b/tests/ut/package.d index 7784fbd..0f34a2a 100644 --- a/tests/ut/package.d +++ b/tests/ut/package.d @@ -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; - } - } -} diff --git a/tests/ut/ref_counted.d b/tests/ut/ref_counted.d index 655bd9d..9ab6120 100644 --- a/tests/ut/ref_counted.d +++ b/tests/ut/ref_counted.d @@ -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") diff --git a/tests/ut/unique.d b/tests/ut/unique.d index 7c4a980..1545181 100644 --- a/tests/ut/unique.d +++ b/tests/ut/unique.d @@ -5,7 +5,11 @@ import ut; import automem.unique; -mixin TestUtils; + +@Shutdown +void after() { + reset; +} /// @@ -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); } @@ -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") @@ -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); } diff --git a/tests/ut/utils.d b/tests/ut/utils.d new file mode 100644 index 0000000..89d5f1a --- /dev/null +++ b/tests/ut/utils.d @@ -0,0 +1,223 @@ +module ut.utils; + +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); + } +} + +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; + } +} + +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; + } +} + +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; + } +} + +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; + } +} + + +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); + } +} + +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; + } + +} + +class NoGcClass { + int i; + static int numClasses = 0; + + this(int i) @safe @nogc nothrow { + this.i = i; + ++numClasses; + } + + ~this() @safe @nogc nothrow { + --numClasses; + } +} + +struct SharedStructWithIndirection { + string s; + this(string s) shared { + this.s = s; + } +}