1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 module tanya.algorithm.tests.mutation; 5 6 import tanya.algorithm.mutation; 7 import tanya.range; 8 import tanya.test.stub; 9 10 // Returns advanced target 11 @nogc nothrow pure @safe unittest 12 { 13 int[5] input = [1, 2, 3, 4, 5]; 14 assert(copy(input[3 .. 5], input[]).front == 3); 15 } 16 17 // Copies overlapping arrays 18 @nogc nothrow pure @safe unittest 19 { 20 import std.algorithm.comparison : equal; 21 22 int[6] actual = [1, 2, 3, 4, 5, 6]; 23 const int[6] expected = [1, 2, 1, 2, 3, 4]; 24 25 copy(actual[0 .. 4], actual[2 .. 6]); 26 assert(equal(actual[], expected[])); 27 } 28 29 @nogc nothrow pure @safe unittest 30 { 31 static assert(is(typeof(copy((ubyte[]).init, (ushort[]).init)))); 32 static assert(!is(typeof(copy((ushort[]).init, (ubyte[]).init)))); 33 } 34 35 @nogc nothrow pure @safe unittest 36 { 37 static struct OutPutRange 38 { 39 int value; 40 41 void opCall(int value) @nogc nothrow pure @safe 42 in 43 { 44 assert(this.value == 0); 45 } 46 do 47 { 48 this.value = value; 49 } 50 } 51 int[1] source = [5]; 52 OutPutRange target; 53 54 assert(copy(source[], target).value == 5); 55 } 56 57 // [] is called where possible 58 @nogc nothrow pure @system unittest 59 { 60 static struct Slice 61 { 62 bool* slicingCalled; 63 64 int front() @nogc nothrow pure @safe 65 { 66 return 0; 67 } 68 69 void front(int) @nogc nothrow pure @safe 70 { 71 } 72 73 void popFront() @nogc nothrow pure @safe 74 { 75 } 76 77 bool empty() @nogc nothrow pure @safe 78 { 79 return true; 80 } 81 82 void opIndexAssign(int) @nogc nothrow pure @safe 83 { 84 *this.slicingCalled = true; 85 } 86 } 87 bool slicingCalled; 88 auto range = Slice(&slicingCalled); 89 fill(range, 0); 90 assert(slicingCalled); 91 } 92 93 @nogc nothrow pure @safe unittest 94 { 95 NonCopyable[] nonCopyable; 96 initializeAll(nonCopyable); 97 }