| 1 |
@safe unittest |
| 2 |
{ |
| 3 |
import std.algorithm.mutation; |
| 4 |
|
| 5 |
auto arr = [4, 5, 6, 7, 1, 2, 3]; |
| 6 |
auto p = bringToFront(arr[0 .. 4], arr[4 .. $]); |
| 7 |
assert(p == arr.length - 4); |
| 8 |
assert(arr == [ 1, 2, 3, 4, 5, 6, 7 ]); |
| 9 |
} |
| 10 |
|
| 11 |
@safe unittest |
| 12 |
{ |
| 13 |
import std.algorithm.mutation; |
| 14 |
|
| 15 |
import std.algorithm.comparison : equal; |
| 16 |
import std.container : SList; |
| 17 |
import std.range.primitives : popFrontN; |
| 18 |
|
| 19 |
auto list = SList!(int)(4, 5, 6, 7, 1, 2, 3); |
| 20 |
auto r1 = list[]; |
| 21 |
auto r2 = list[]; popFrontN(r2, 4); |
| 22 |
assert(equal(r2, [ 1, 2, 3 ])); |
| 23 |
bringToFront(r1, r2); |
| 24 |
assert(equal(list[], [ 1, 2, 3, 4, 5, 6, 7 ])); |
| 25 |
} |
| 26 |
|
| 27 |
@safe unittest |
| 28 |
{ |
| 29 |
import std.algorithm.mutation; |
| 30 |
|
| 31 |
import std.algorithm.comparison : equal; |
| 32 |
import std.container : SList; |
| 33 |
|
| 34 |
auto list = SList!(int)(4, 5, 6, 7); |
| 35 |
auto vec = [ 1, 2, 3 ]; |
| 36 |
bringToFront(list[], vec); |
| 37 |
assert(equal(list[], [ 1, 2, 3, 4 ])); |
| 38 |
assert(equal(vec, [ 5, 6, 7 ])); |
| 39 |
} |
| 40 |
|
| 41 |
@safe unittest |
| 42 |
{ |
| 43 |
import std.algorithm.mutation; |
| 44 |
|
| 45 |
import std.string : representation; |
| 46 |
auto ar = representation("a".dup); |
| 47 |
auto br = representation("ç".dup); |
| 48 |
|
| 49 |
bringToFront(ar, br); |
| 50 |
|
| 51 |
auto a = cast(char[]) ar; |
| 52 |
auto b = cast(char[]) br; |
| 53 |
|
| 54 |
// Illegal UTF-8 |
| 55 |
assert(a == "\303"); |
| 56 |
// Illegal UTF-8 |
| 57 |
assert(b == "\247a"); |
| 58 |
} |
| 59 |
|
| 60 |
@safe unittest |
| 61 |
{ |
| 62 |
import std.algorithm.mutation; |
| 63 |
|
| 64 |
int[] a = [ 1, 5 ]; |
| 65 |
int[] b = [ 9, 8 ]; |
| 66 |
int[] buf = new int[](a.length + b.length + 10); |
| 67 |
auto rem = a.copy(buf); // copy a into buf |
| 68 |
rem = b.copy(rem); // copy b into remainder of buf |
| 69 |
assert(buf[0 .. a.length + b.length] == [1, 5, 9, 8]); |
| 70 |
assert(rem.length == 10); // unused slots in buf |
| 71 |
} |
| 72 |
|
| 73 |
@safe unittest |
| 74 |
{ |
| 75 |
import std.algorithm.mutation; |
| 76 |
|
| 77 |
float[] src = [ 1.0f, 5 ]; |
| 78 |
double[] dest = new double[src.length]; |
| 79 |
src.copy(dest); |
| 80 |
} |
| 81 |
|
| 82 |
@safe unittest |
| 83 |
{ |
| 84 |
import std.algorithm.mutation; |
| 85 |
|
| 86 |
import std.range; |
| 87 |
int[] src = [ 1, 5, 8, 9, 10 ]; |
| 88 |
auto dest = new int[](3); |
| 89 |
src.take(dest.length).copy(dest); |
| 90 |
assert(dest == [ 1, 5, 8 ]); |
| 91 |
} |
| 92 |
|
| 93 |
@safe unittest |
| 94 |
{ |
| 95 |
import std.algorithm.mutation; |
| 96 |
|
| 97 |
import std.algorithm.iteration : filter; |
| 98 |
int[] src = [ 1, 5, 8, 9, 10, 1, 2, 0 ]; |
| 99 |
auto dest = new int[src.length]; |
| 100 |
auto rem = src |
| 101 |
.filter!(a => (a & 1) == 1) |
| 102 |
.copy(dest); |
| 103 |
assert(dest[0 .. $ - rem.length] == [ 1, 5, 9, 1 ]); |
| 104 |
} |
| 105 |
|
| 106 |
@safe unittest |
| 107 |
{ |
| 108 |
import std.algorithm.mutation; |
| 109 |
|
| 110 |
import std.algorithm, std.range; |
| 111 |
int[] src = [1, 2, 4]; |
| 112 |
int[] dest = [0, 0, 0, 0, 0]; |
| 113 |
src.retro.copy(dest.retro); |
| 114 |
assert(dest == [0, 0, 1, 2, 4]); |
| 115 |
} |
| 116 |
|
| 117 |
@safe unittest |
| 118 |
{ |
| 119 |
import std.algorithm.mutation; |
| 120 |
|
| 121 |
int[] a = [ 1, 2, 3, 4 ]; |
| 122 |
fill(a, 5); |
| 123 |
assert(a == [ 5, 5, 5, 5 ]); |
| 124 |
} |
| 125 |
|
| 126 |
@safe unittest |
| 127 |
{ |
| 128 |
import std.algorithm.mutation; |
| 129 |
|
| 130 |
int[] a = [ 1, 2, 3, 4, 5 ]; |
| 131 |
int[] b = [ 8, 9 ]; |
| 132 |
fill(a, b); |
| 133 |
assert(a == [ 8, 9, 8, 9, 8 ]); |
| 134 |
} |
| 135 |
|
| 136 |
@system unittest |
| 137 |
{ |
| 138 |
import std.algorithm.mutation; |
| 139 |
|
| 140 |
import core.stdc.stdlib : malloc, free; |
| 141 |
|
| 142 |
struct S |
| 143 |
{ |
| 144 |
int a = 10; |
| 145 |
} |
| 146 |
|
| 147 |
auto s = (cast(S*) malloc(5 * S.sizeof))[0 .. 5]; |
| 148 |
initializeAll(s); |
| 149 |
assert(s == [S(10), S(10), S(10), S(10), S(10)]); |
| 150 |
|
| 151 |
scope(exit) free(s.ptr); |
| 152 |
} |
| 153 |
|
| 154 |
@safe unittest |
| 155 |
{ |
| 156 |
import std.algorithm.mutation; |
| 157 |
|
| 158 |
Object obj1 = new Object; |
| 159 |
Object obj2 = obj1; |
| 160 |
Object obj3; |
| 161 |
|
| 162 |
move(obj2, obj3); |
| 163 |
assert(obj3 is obj1); |
| 164 |
// obj2 unchanged |
| 165 |
assert(obj2 is obj1); |
| 166 |
} |
| 167 |
|
| 168 |
pure nothrow @safe @nogc unittest |
| 169 |
{ |
| 170 |
import std.algorithm.mutation; |
| 171 |
|
| 172 |
// Structs without destructors are simply copied |
| 173 |
struct S1 |
| 174 |
{ |
| 175 |
int a = 1; |
| 176 |
int b = 2; |
| 177 |
} |
| 178 |
S1 s11 = { 10, 11 }; |
| 179 |
S1 s12; |
| 180 |
|
| 181 |
move(s11, s12); |
| 182 |
|
| 183 |
assert(s12 == S1(10, 11)); |
| 184 |
assert(s11 == s12); |
| 185 |
|
| 186 |
// But structs with destructors or postblits are reset to their .init value |
| 187 |
// after copying to the target. |
| 188 |
struct S2 |
| 189 |
{ |
| 190 |
int a = 1; |
| 191 |
int b = 2; |
| 192 |
|
| 193 |
~this() pure nothrow @safe @nogc { } |
| 194 |
} |
| 195 |
S2 s21 = { 3, 4 }; |
| 196 |
S2 s22; |
| 197 |
|
| 198 |
move(s21, s22); |
| 199 |
|
| 200 |
assert(s21 == S2(1, 2)); |
| 201 |
assert(s22 == S2(3, 4)); |
| 202 |
} |
| 203 |
|
| 204 |
pure nothrow @safe @nogc unittest |
| 205 |
{ |
| 206 |
import std.algorithm.mutation; |
| 207 |
|
| 208 |
struct S |
| 209 |
{ |
| 210 |
int a = 1; |
| 211 |
@disable this(this); |
| 212 |
~this() pure nothrow @safe @nogc {} |
| 213 |
} |
| 214 |
S s1; |
| 215 |
s1.a = 2; |
| 216 |
S s2 = move(s1); |
| 217 |
assert(s1.a == 1); |
| 218 |
assert(s2.a == 2); |
| 219 |
} |
| 220 |
|
| 221 |
pure nothrow @safe @nogc unittest |
| 222 |
{ |
| 223 |
import std.algorithm.mutation; |
| 224 |
|
| 225 |
struct S |
| 226 |
{ |
| 227 |
int a; |
| 228 |
void opPostMove(const ref S old) |
| 229 |
{ |
| 230 |
assert(a == old.a); |
| 231 |
a++; |
| 232 |
} |
| 233 |
} |
| 234 |
S s1; |
| 235 |
s1.a = 41; |
| 236 |
S s2 = move(s1); |
| 237 |
assert(s2.a == 42); |
| 238 |
} |
| 239 |
|
| 240 |
pure nothrow @nogc @system unittest |
| 241 |
{ |
| 242 |
import std.algorithm.mutation; |
| 243 |
|
| 244 |
static struct Foo |
| 245 |
{ |
| 246 |
pure nothrow @nogc: |
| 247 |
this(int* ptr) { _ptr = ptr; } |
| 248 |
~this() { if (_ptr) ++*_ptr; } |
| 249 |
int* _ptr; |
| 250 |
} |
| 251 |
|
| 252 |
int val; |
| 253 |
Foo foo1 = void; // uninitialized |
| 254 |
auto foo2 = Foo(&val); // initialized |
| 255 |
assert(foo2._ptr is &val); |
| 256 |
|
| 257 |
// Using `move(foo2, foo1)` would have an undefined effect because it would destroy |
| 258 |
// the uninitialized foo1. |
| 259 |
// moveEmplace directly overwrites foo1 without destroying or initializing it first. |
| 260 |
moveEmplace(foo2, foo1); |
| 261 |
assert(foo1._ptr is &val); |
| 262 |
assert(foo2._ptr is null); |
| 263 |
assert(val == 0); |
| 264 |
} |
| 265 |
|
| 266 |
pure nothrow @safe @nogc unittest |
| 267 |
{ |
| 268 |
import std.algorithm.mutation; |
| 269 |
|
| 270 |
int[3] a = [ 1, 2, 3 ]; |
| 271 |
int[5] b; |
| 272 |
assert(moveAll(a[], b[]) is b[3 .. $]); |
| 273 |
assert(a[] == b[0 .. 3]); |
| 274 |
int[3] cmp = [ 1, 2, 3 ]; |
| 275 |
assert(a[] == cmp[]); |
| 276 |
} |
| 277 |
|
| 278 |
pure nothrow @nogc @system unittest |
| 279 |
{ |
| 280 |
import std.algorithm.mutation; |
| 281 |
|
| 282 |
static struct Foo |
| 283 |
{ |
| 284 |
~this() pure nothrow @nogc { if (_ptr) ++*_ptr; } |
| 285 |
int* _ptr; |
| 286 |
} |
| 287 |
int[3] refs = [0, 1, 2]; |
| 288 |
Foo[3] src = [Foo(&refs[0]), Foo(&refs[1]), Foo(&refs[2])]; |
| 289 |
Foo[5] dst = void; |
| 290 |
|
| 291 |
auto tail = moveEmplaceAll(src[], dst[]); // move 3 value from src over dst |
| 292 |
assert(tail.length == 2); // returns remaining uninitialized values |
| 293 |
initializeAll(tail); |
| 294 |
|
| 295 |
import std.algorithm.searching : all; |
| 296 |
assert(src[].all!(e => e._ptr is null)); |
| 297 |
assert(dst[0 .. 3].all!(e => e._ptr !is null)); |
| 298 |
} |
| 299 |
|
| 300 |
pure nothrow @safe @nogc unittest |
| 301 |
{ |
| 302 |
import std.algorithm.mutation; |
| 303 |
|
| 304 |
int[5] a = [ 1, 2, 3, 4, 5 ]; |
| 305 |
int[3] b; |
| 306 |
assert(moveSome(a[], b[])[0] is a[3 .. $]); |
| 307 |
assert(a[0 .. 3] == b); |
| 308 |
assert(a == [ 1, 2, 3, 4, 5 ]); |
| 309 |
} |
| 310 |
|
| 311 |
pure nothrow @nogc @system unittest |
| 312 |
{ |
| 313 |
import std.algorithm.mutation; |
| 314 |
|
| 315 |
static struct Foo |
| 316 |
{ |
| 317 |
~this() pure nothrow @nogc { if (_ptr) ++*_ptr; } |
| 318 |
int* _ptr; |
| 319 |
} |
| 320 |
int[4] refs = [0, 1, 2, 3]; |
| 321 |
Foo[4] src = [Foo(&refs[0]), Foo(&refs[1]), Foo(&refs[2]), Foo(&refs[3])]; |
| 322 |
Foo[3] dst = void; |
| 323 |
|
| 324 |
auto res = moveEmplaceSome(src[], dst[]); |
| 325 |
assert(res.length == 2); |
| 326 |
|
| 327 |
import std.algorithm.searching : all; |
| 328 |
assert(src[0 .. 3].all!(e => e._ptr is null)); |
| 329 |
assert(src[3]._ptr !is null); |
| 330 |
assert(dst[].all!(e => e._ptr !is null)); |
| 331 |
} |
| 332 |
|
| 333 |
@safe unittest |
| 334 |
{ |
| 335 |
import std.algorithm.mutation; |
| 336 |
|
| 337 |
int[] a = [0, 1, 2, 3]; |
| 338 |
assert(remove!(SwapStrategy.stable)(a, 1) == [0, 2, 3]); |
| 339 |
a = [0, 1, 2, 3]; |
| 340 |
assert(remove!(SwapStrategy.unstable)(a, 1) == [0, 3, 2]); |
| 341 |
} |
| 342 |
|
| 343 |
@safe unittest |
| 344 |
{ |
| 345 |
import std.algorithm.mutation; |
| 346 |
|
| 347 |
import std.algorithm.sorting : partition; |
| 348 |
|
| 349 |
// Put stuff greater than 3 on the left |
| 350 |
auto arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
| 351 |
assert(partition!(a => a > 3, SwapStrategy.stable)(arr) == [1, 2, 3]); |
| 352 |
assert(arr == [4, 5, 6, 7, 8, 9, 10, 1, 2, 3]); |
| 353 |
|
| 354 |
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
| 355 |
assert(partition!(a => a > 3, SwapStrategy.semistable)(arr) == [2, 3, 1]); |
| 356 |
assert(arr == [4, 5, 6, 7, 8, 9, 10, 2, 3, 1]); |
| 357 |
|
| 358 |
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
| 359 |
assert(partition!(a => a > 3, SwapStrategy.unstable)(arr) == [3, 2, 1]); |
| 360 |
assert(arr == [10, 9, 8, 4, 5, 6, 7, 3, 2, 1]); |
| 361 |
} |
| 362 |
|
| 363 |
@safe pure unittest |
| 364 |
{ |
| 365 |
import std.algorithm.mutation; |
| 366 |
|
| 367 |
import std.typecons : tuple; |
| 368 |
|
| 369 |
auto a = [ 0, 1, 2, 3, 4, 5 ]; |
| 370 |
assert(remove!(SwapStrategy.stable)(a, 1) == [ 0, 2, 3, 4, 5 ]); |
| 371 |
a = [ 0, 1, 2, 3, 4, 5 ]; |
| 372 |
assert(remove!(SwapStrategy.stable)(a, 1, 3) == [ 0, 2, 4, 5] ); |
| 373 |
a = [ 0, 1, 2, 3, 4, 5 ]; |
| 374 |
assert(remove!(SwapStrategy.stable)(a, 1, tuple(3, 6)) == [ 0, 2 ]); |
| 375 |
|
| 376 |
a = [ 0, 1, 2, 3, 4, 5 ]; |
| 377 |
assert(remove!(SwapStrategy.unstable)(a, 1) == [0, 5, 2, 3, 4]); |
| 378 |
a = [ 0, 1, 2, 3, 4, 5 ]; |
| 379 |
assert(remove!(SwapStrategy.unstable)(a, tuple(1, 4)) == [0, 5, 4]); |
| 380 |
} |
| 381 |
|
| 382 |
@safe pure unittest |
| 383 |
{ |
| 384 |
import std.algorithm.mutation; |
| 385 |
|
| 386 |
import std.typecons : tuple; |
| 387 |
|
| 388 |
// Delete an index |
| 389 |
assert([4, 5, 6].remove(1) == [4, 6]); |
| 390 |
|
| 391 |
// Delete multiple indices |
| 392 |
assert([4, 5, 6, 7, 8].remove(1, 3) == [4, 6, 8]); |
| 393 |
|
| 394 |
// Use an indices range |
| 395 |
assert([4, 5, 6, 7, 8].remove(tuple(1, 3)) == [4, 7, 8]); |
| 396 |
|
| 397 |
// Use an indices range and individual indices |
| 398 |
assert([4, 5, 6, 7, 8].remove(0, tuple(1, 3), 4) == [7]); |
| 399 |
} |
| 400 |
|
| 401 |
@safe pure unittest |
| 402 |
{ |
| 403 |
import std.algorithm.mutation; |
| 404 |
|
| 405 |
assert([5, 6, 7, 8].remove!(SwapStrategy.stable)(1) == [5, 7, 8]); |
| 406 |
assert([5, 6, 7, 8].remove!(SwapStrategy.unstable)(1) == [5, 8, 7]); |
| 407 |
} |
| 408 |
|
| 409 |
@safe unittest |
| 410 |
{ |
| 411 |
import std.algorithm.mutation; |
| 412 |
|
| 413 |
static immutable base = [1, 2, 3, 2, 4, 2, 5, 2]; |
| 414 |
|
| 415 |
int[] arr = base[].dup; |
| 416 |
|
| 417 |
// using a string-based predicate |
| 418 |
assert(remove!("a == 2")(arr) == [ 1, 3, 4, 5 ]); |
| 419 |
|
| 420 |
// The original array contents have been modified, |
| 421 |
// so we need to reset it to its original state. |
| 422 |
// The length is unmodified however. |
| 423 |
arr[] = base[]; |
| 424 |
|
| 425 |
// using a lambda predicate |
| 426 |
assert(remove!(a => a == 2)(arr) == [ 1, 3, 4, 5 ]); |
| 427 |
} |
| 428 |
|
| 429 |
@safe unittest |
| 430 |
{ |
| 431 |
import std.algorithm.mutation; |
| 432 |
|
| 433 |
int[] arr = [ 1, 2, 3 ]; |
| 434 |
assert(arr.reverse == [ 3, 2, 1 ]); |
| 435 |
} |
| 436 |
|
| 437 |
@safe unittest |
| 438 |
{ |
| 439 |
import std.algorithm.mutation; |
| 440 |
|
| 441 |
char[] arr = "hello\U00010143\u0100\U00010143".dup; |
| 442 |
assert(arr.reverse == "\U00010143\u0100\U00010143olleh"); |
| 443 |
} |
| 444 |
|
| 445 |
@safe pure unittest |
| 446 |
{ |
| 447 |
import std.algorithm.mutation; |
| 448 |
|
| 449 |
assert(" foobar ".strip(' ') == "foobar"); |
| 450 |
assert("00223.444500".strip('0') == "223.4445"); |
| 451 |
assert("ëëêéüŗōpéêëë".strip('ë') == "êéüŗōpéê"); |
| 452 |
assert([1, 1, 0, 1, 1].strip(1) == [0]); |
| 453 |
assert([0.0, 0.01, 0.01, 0.0].strip(0).length == 2); |
| 454 |
} |
| 455 |
|
| 456 |
@safe pure unittest |
| 457 |
{ |
| 458 |
import std.algorithm.mutation; |
| 459 |
|
| 460 |
assert(" foobar ".strip!(a => a == ' ')() == "foobar"); |
| 461 |
assert("00223.444500".strip!(a => a == '0')() == "223.4445"); |
| 462 |
assert("ëëêéüŗōpéêëë".strip!(a => a == 'ë')() == "êéüŗōpéê"); |
| 463 |
assert([1, 1, 0, 1, 1].strip!(a => a == 1)() == [0]); |
| 464 |
assert([0.0, 0.01, 0.5, 0.6, 0.01, 0.0].strip!(a => a < 0.4)().length == 2); |
| 465 |
} |
| 466 |
|
| 467 |
@safe pure unittest |
| 468 |
{ |
| 469 |
import std.algorithm.mutation; |
| 470 |
|
| 471 |
assert(" foobar ".stripLeft(' ') == "foobar "); |
| 472 |
assert("00223.444500".stripLeft('0') == "223.444500"); |
| 473 |
assert("ůůűniçodêéé".stripLeft('ů') == "űniçodêéé"); |
| 474 |
assert([1, 1, 0, 1, 1].stripLeft(1) == [0, 1, 1]); |
| 475 |
assert([0.0, 0.01, 0.01, 0.0].stripLeft(0).length == 3); |
| 476 |
} |
| 477 |
|
| 478 |
@safe pure unittest |
| 479 |
{ |
| 480 |
import std.algorithm.mutation; |
| 481 |
|
| 482 |
assert(" foobar ".stripLeft!(a => a == ' ')() == "foobar "); |
| 483 |
assert("00223.444500".stripLeft!(a => a == '0')() == "223.444500"); |
| 484 |
assert("ůůűniçodêéé".stripLeft!(a => a == 'ů')() == "űniçodêéé"); |
| 485 |
assert([1, 1, 0, 1, 1].stripLeft!(a => a == 1)() == [0, 1, 1]); |
| 486 |
assert([0.0, 0.01, 0.10, 0.5, 0.6].stripLeft!(a => a < 0.4)().length == 2); |
| 487 |
} |
| 488 |
|
| 489 |
@safe pure unittest |
| 490 |
{ |
| 491 |
import std.algorithm.mutation; |
| 492 |
|
| 493 |
assert(" foobar ".stripRight(' ') == " foobar"); |
| 494 |
assert("00223.444500".stripRight('0') == "00223.4445"); |
| 495 |
assert("ùniçodêéé".stripRight('é') == "ùniçodê"); |
| 496 |
assert([1, 1, 0, 1, 1].stripRight(1) == [1, 1, 0]); |
| 497 |
assert([0.0, 0.01, 0.01, 0.0].stripRight(0).length == 3); |
| 498 |
} |
| 499 |
|
| 500 |
@safe pure unittest |
| 501 |
{ |
| 502 |
import std.algorithm.mutation; |
| 503 |
|
| 504 |
assert(" foobar ".stripRight!(a => a == ' ')() == " foobar"); |
| 505 |
assert("00223.444500".stripRight!(a => a == '0')() == "00223.4445"); |
| 506 |
assert("ùniçodêéé".stripRight!(a => a == 'é')() == "ùniçodê"); |
| 507 |
assert([1, 1, 0, 1, 1].stripRight!(a => a == 1)() == [1, 1, 0]); |
| 508 |
assert([0.0, 0.01, 0.10, 0.5, 0.6].stripRight!(a => a > 0.4)().length == 3); |
| 509 |
} |
| 510 |
|
| 511 |
@safe unittest |
| 512 |
{ |
| 513 |
import std.algorithm.mutation; |
| 514 |
|
| 515 |
// Swapping POD (plain old data) types: |
| 516 |
int a = 42, b = 34; |
| 517 |
swap(a, b); |
| 518 |
assert(a == 34 && b == 42); |
| 519 |
|
| 520 |
// Swapping structs with indirection: |
| 521 |
static struct S { int x; char c; int[] y; } |
| 522 |
S s1 = { 0, 'z', [ 1, 2 ] }; |
| 523 |
S s2 = { 42, 'a', [ 4, 6 ] }; |
| 524 |
swap(s1, s2); |
| 525 |
assert(s1.x == 42); |
| 526 |
assert(s1.c == 'a'); |
| 527 |
assert(s1.y == [ 4, 6 ]); |
| 528 |
|
| 529 |
assert(s2.x == 0); |
| 530 |
assert(s2.c == 'z'); |
| 531 |
assert(s2.y == [ 1, 2 ]); |
| 532 |
|
| 533 |
// Immutables cannot be swapped: |
| 534 |
immutable int imm1 = 1, imm2 = 2; |
| 535 |
static assert(!__traits(compiles, swap(imm1, imm2))); |
| 536 |
|
| 537 |
int c = imm1 + 0; |
| 538 |
int d = imm2 + 0; |
| 539 |
swap(c, d); |
| 540 |
assert(c == 2); |
| 541 |
assert(d == 1); |
| 542 |
} |
| 543 |
|
| 544 |
@safe unittest |
| 545 |
{ |
| 546 |
import std.algorithm.mutation; |
| 547 |
|
| 548 |
// Non-copyable types can still be swapped. |
| 549 |
static struct NoCopy |
| 550 |
{ |
| 551 |
this(this) { assert(0); } |
| 552 |
int n; |
| 553 |
string s; |
| 554 |
} |
| 555 |
NoCopy nc1, nc2; |
| 556 |
nc1.n = 127; nc1.s = "abc"; |
| 557 |
nc2.n = 513; nc2.s = "uvwxyz"; |
| 558 |
|
| 559 |
swap(nc1, nc2); |
| 560 |
assert(nc1.n == 513 && nc1.s == "uvwxyz"); |
| 561 |
assert(nc2.n == 127 && nc2.s == "abc"); |
| 562 |
|
| 563 |
swap(nc1, nc1); |
| 564 |
swap(nc2, nc2); |
| 565 |
assert(nc1.n == 513 && nc1.s == "uvwxyz"); |
| 566 |
assert(nc2.n == 127 && nc2.s == "abc"); |
| 567 |
|
| 568 |
// Types containing non-copyable fields can also be swapped. |
| 569 |
static struct NoCopyHolder |
| 570 |
{ |
| 571 |
NoCopy noCopy; |
| 572 |
} |
| 573 |
NoCopyHolder h1, h2; |
| 574 |
h1.noCopy.n = 31; h1.noCopy.s = "abc"; |
| 575 |
h2.noCopy.n = 65; h2.noCopy.s = null; |
| 576 |
|
| 577 |
swap(h1, h2); |
| 578 |
assert(h1.noCopy.n == 65 && h1.noCopy.s == null); |
| 579 |
assert(h2.noCopy.n == 31 && h2.noCopy.s == "abc"); |
| 580 |
|
| 581 |
swap(h1, h1); |
| 582 |
swap(h2, h2); |
| 583 |
assert(h1.noCopy.n == 65 && h1.noCopy.s == null); |
| 584 |
assert(h2.noCopy.n == 31 && h2.noCopy.s == "abc"); |
| 585 |
|
| 586 |
// Const types cannot be swapped. |
| 587 |
const NoCopy const1, const2; |
| 588 |
assert(const1.n == 0 && const2.n == 0); |
| 589 |
static assert(!__traits(compiles, swap(const1, const2))); |
| 590 |
} |
| 591 |
|
| 592 |
pure @safe nothrow unittest |
| 593 |
{ |
| 594 |
import std.algorithm.mutation; |
| 595 |
|
| 596 |
import std.algorithm.comparison : equal; |
| 597 |
auto a = [1, 2, 3]; |
| 598 |
a.swapAt(1, 2); |
| 599 |
assert(a.equal([1, 3, 2])); |
| 600 |
} |
| 601 |
|
| 602 |
@safe unittest |
| 603 |
{ |
| 604 |
import std.algorithm.mutation; |
| 605 |
|
| 606 |
import std.range : empty; |
| 607 |
int[] a = [ 100, 101, 102, 103 ]; |
| 608 |
int[] b = [ 0, 1, 2, 3 ]; |
| 609 |
auto c = swapRanges(a[1 .. 3], b[2 .. 4]); |
| 610 |
assert(c[0].empty && c[1].empty); |
| 611 |
assert(a == [ 100, 2, 3, 103 ]); |
| 612 |
assert(b == [ 0, 1, 101, 102 ]); |
| 613 |
} |
| 614 |
|
| 615 |
nothrow @system unittest |
| 616 |
{ |
| 617 |
import std.algorithm.mutation; |
| 618 |
|
| 619 |
import core.stdc.stdlib : malloc, free; |
| 620 |
|
| 621 |
auto s = (cast(int*) malloc(5 * int.sizeof))[0 .. 5]; |
| 622 |
uninitializedFill(s, 42); |
| 623 |
assert(s == [ 42, 42, 42, 42, 42 ]); |
| 624 |
|
| 625 |
scope(exit) free(s.ptr); |
| 626 |
} |
| 627 |
|