| 1 |
@safe @nogc unittest |
| 2 |
{ |
| 3 |
import std.algorithm.comparison; |
| 4 |
|
| 5 |
assert(3.among(1, 42, 24, 3, 2)); |
| 6 |
|
| 7 |
if (auto pos = "bar".among("foo", "bar", "baz")) |
| 8 |
assert(pos == 2); |
| 9 |
else |
| 10 |
assert(false); |
| 11 |
|
| 12 |
// 42 is larger than 24 |
| 13 |
assert(42.among!((lhs, rhs) => lhs > rhs)(43, 24, 100) == 2); |
| 14 |
} |
| 15 |
|
| 16 |
@safe @nogc unittest |
| 17 |
{ |
| 18 |
import std.algorithm.comparison; |
| 19 |
|
| 20 |
assert(3.among!(2, 3, 4)); |
| 21 |
assert("bar".among!("foo", "bar", "baz") == 2); |
| 22 |
} |
| 23 |
|
| 24 |
@system unittest |
| 25 |
{ |
| 26 |
import std.algorithm.comparison; |
| 27 |
|
| 28 |
import std.algorithm.iteration : map; |
| 29 |
import std.format : format; |
| 30 |
|
| 31 |
class A |
| 32 |
{ |
| 33 |
int a; |
| 34 |
this(int a) {this.a = a;} |
| 35 |
@property int i() { return a; } |
| 36 |
} |
| 37 |
interface I { } |
| 38 |
class B : I { } |
| 39 |
|
| 40 |
Object[] arr = [new A(1), new B(), null]; |
| 41 |
|
| 42 |
auto results = arr.map!(castSwitch!( |
| 43 |
(A a) => "A with a value of %d".format(a.a), |
| 44 |
(I i) => "derived from I", |
| 45 |
() => "null reference", |
| 46 |
))(); |
| 47 |
|
| 48 |
// A is handled directly: |
| 49 |
assert(results[0] == "A with a value of 1"); |
| 50 |
// B has no handler - it is handled by the handler of I: |
| 51 |
assert(results[1] == "derived from I"); |
| 52 |
// null is handled by the null handler: |
| 53 |
assert(results[2] == "null reference"); |
| 54 |
} |
| 55 |
|
| 56 |
@system unittest |
| 57 |
{ |
| 58 |
import std.algorithm.comparison; |
| 59 |
|
| 60 |
import std.exception : assertThrown; |
| 61 |
|
| 62 |
class A { } |
| 63 |
class B { } |
| 64 |
// Void handlers are allowed if they throw: |
| 65 |
assertThrown!Exception( |
| 66 |
new B().castSwitch!( |
| 67 |
(A a) => 1, |
| 68 |
(B d) { throw new Exception("B is not allowed!"); } |
| 69 |
)() |
| 70 |
); |
| 71 |
|
| 72 |
// Void handlers are also allowed if all the handlers are void: |
| 73 |
new A().castSwitch!( |
| 74 |
(A a) { }, |
| 75 |
(B b) { assert(false); }, |
| 76 |
)(); |
| 77 |
} |
| 78 |
|
| 79 |
@safe @nogc unittest |
| 80 |
{ |
| 81 |
import std.algorithm.comparison; |
| 82 |
|
| 83 |
assert(clamp(2, 1, 3) == 2); |
| 84 |
assert(clamp(0, 1, 3) == 1); |
| 85 |
assert(clamp(4, 1, 3) == 3); |
| 86 |
|
| 87 |
assert(clamp(1, 1, 1) == 1); |
| 88 |
|
| 89 |
assert(clamp(5, -1, 2u) == 2); |
| 90 |
|
| 91 |
auto x = clamp(42, uint.max, uint.max); |
| 92 |
static assert(is(typeof(x) == int)); |
| 93 |
assert(x == -1); |
| 94 |
} |
| 95 |
|
| 96 |
pure @safe unittest |
| 97 |
{ |
| 98 |
import std.algorithm.comparison; |
| 99 |
|
| 100 |
int result; |
| 101 |
|
| 102 |
result = cmp("abc", "abc"); |
| 103 |
assert(result == 0); |
| 104 |
result = cmp("", ""); |
| 105 |
assert(result == 0); |
| 106 |
result = cmp("abc", "abcd"); |
| 107 |
assert(result < 0); |
| 108 |
result = cmp("abcd", "abc"); |
| 109 |
assert(result > 0); |
| 110 |
result = cmp("abc"d, "abd"); |
| 111 |
assert(result < 0); |
| 112 |
result = cmp("bbc", "abc"w); |
| 113 |
assert(result > 0); |
| 114 |
result = cmp("aaa", "aaaa"d); |
| 115 |
assert(result < 0); |
| 116 |
result = cmp("aaaa", "aaa"d); |
| 117 |
assert(result > 0); |
| 118 |
result = cmp("aaa", "aaa"d); |
| 119 |
assert(result == 0); |
| 120 |
result = cmp("aaa"d, "aaa"d); |
| 121 |
assert(result == 0); |
| 122 |
result = cmp(cast(int[])[], cast(int[])[]); |
| 123 |
assert(result == 0); |
| 124 |
result = cmp([1, 2, 3], [1, 2, 3]); |
| 125 |
assert(result == 0); |
| 126 |
result = cmp([1, 3, 2], [1, 2, 3]); |
| 127 |
assert(result > 0); |
| 128 |
result = cmp([1, 2, 3], [1L, 2, 3, 4]); |
| 129 |
assert(result < 0); |
| 130 |
result = cmp([1L, 2, 3], [1, 2]); |
| 131 |
assert(result > 0); |
| 132 |
} |
| 133 |
|
| 134 |
pure @safe unittest |
| 135 |
{ |
| 136 |
import std.algorithm.comparison; |
| 137 |
|
| 138 |
int result; |
| 139 |
|
| 140 |
result = cmp!"a > b"("abc", "abc"); |
| 141 |
assert(result == 0); |
| 142 |
result = cmp!"a > b"("", ""); |
| 143 |
assert(result == 0); |
| 144 |
result = cmp!"a > b"("abc", "abcd"); |
| 145 |
assert(result < 0); |
| 146 |
result = cmp!"a > b"("abcd", "abc"); |
| 147 |
assert(result > 0); |
| 148 |
result = cmp!"a > b"("abc"d, "abd"); |
| 149 |
assert(result > 0); |
| 150 |
result = cmp!"a > b"("bbc", "abc"w); |
| 151 |
assert(result < 0); |
| 152 |
result = cmp!"a > b"("aaa", "aaaa"d); |
| 153 |
assert(result < 0); |
| 154 |
result = cmp!"a > b"("aaaa", "aaa"d); |
| 155 |
assert(result > 0); |
| 156 |
result = cmp!"a > b"("aaa", "aaa"d); |
| 157 |
assert(result == 0); |
| 158 |
result = cmp("aaa"d, "aaa"d); |
| 159 |
assert(result == 0); |
| 160 |
result = cmp!"a > b"(cast(int[])[], cast(int[])[]); |
| 161 |
assert(result == 0); |
| 162 |
result = cmp!"a > b"([1, 2, 3], [1, 2, 3]); |
| 163 |
assert(result == 0); |
| 164 |
result = cmp!"a > b"([1, 3, 2], [1, 2, 3]); |
| 165 |
assert(result < 0); |
| 166 |
result = cmp!"a > b"([1, 2, 3], [1L, 2, 3, 4]); |
| 167 |
assert(result < 0); |
| 168 |
result = cmp!"a > b"([1L, 2, 3], [1, 2]); |
| 169 |
assert(result > 0); |
| 170 |
} |
| 171 |
|
| 172 |
@safe @nogc unittest |
| 173 |
{ |
| 174 |
import std.algorithm.comparison; |
| 175 |
|
| 176 |
import std.algorithm.comparison : equal; |
| 177 |
import std.math.operations : isClose; |
| 178 |
|
| 179 |
int[4] a = [ 1, 2, 4, 3 ]; |
| 180 |
assert(!equal(a[], a[1..$])); |
| 181 |
assert(equal(a[], a[])); |
| 182 |
assert(equal!((a, b) => a == b)(a[], a[])); |
| 183 |
|
| 184 |
// different types |
| 185 |
double[4] b = [ 1.0, 2, 4, 3]; |
| 186 |
assert(!equal(a[], b[1..$])); |
| 187 |
assert(equal(a[], b[])); |
| 188 |
|
| 189 |
// predicated: ensure that two vectors are approximately equal |
| 190 |
double[4] c = [ 1.0000000005, 2, 4, 3]; |
| 191 |
assert(equal!isClose(b[], c[])); |
| 192 |
} |
| 193 |
|
| 194 |
@safe unittest |
| 195 |
{ |
| 196 |
import std.algorithm.comparison; |
| 197 |
|
| 198 |
import std.algorithm.comparison : equal; |
| 199 |
import std.range : iota, chunks; |
| 200 |
assert(equal!(equal!equal)( |
| 201 |
[[[0, 1], [2, 3]], [[4, 5], [6, 7]]], |
| 202 |
iota(0, 8).chunks(2).chunks(2) |
| 203 |
)); |
| 204 |
} |
| 205 |
|
| 206 |
@safe unittest |
| 207 |
{ |
| 208 |
import std.algorithm.comparison; |
| 209 |
|
| 210 |
with(EditOp) |
| 211 |
{ |
| 212 |
assert(levenshteinDistanceAndPath("foo", "foobar")[1] == [none, none, none, insert, insert, insert]); |
| 213 |
assert(levenshteinDistanceAndPath("banana", "fazan")[1] == [substitute, none, substitute, none, none, remove]); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
@safe unittest |
| 218 |
{ |
| 219 |
import std.algorithm.comparison; |
| 220 |
|
| 221 |
import std.algorithm.iteration : filter; |
| 222 |
import std.uni : toUpper; |
| 223 |
|
| 224 |
assert(levenshteinDistance("cat", "rat") == 1); |
| 225 |
assert(levenshteinDistance("parks", "spark") == 2); |
| 226 |
assert(levenshteinDistance("abcde", "abcde") == 0); |
| 227 |
assert(levenshteinDistance("abcde", "abCde") == 1); |
| 228 |
assert(levenshteinDistance("kitten", "sitting") == 3); |
| 229 |
assert(levenshteinDistance!((a, b) => toUpper(a) == toUpper(b)) |
| 230 |
("parks", "SPARK") == 2); |
| 231 |
assert(levenshteinDistance("parks".filter!"true", "spark".filter!"true") == 2); |
| 232 |
assert(levenshteinDistance("ID", "I♥D") == 1); |
| 233 |
} |
| 234 |
|
| 235 |
@safe unittest |
| 236 |
{ |
| 237 |
import std.algorithm.comparison; |
| 238 |
|
| 239 |
string a = "Saturday", b = "Sundays"; |
| 240 |
auto p = levenshteinDistanceAndPath(a, b); |
| 241 |
assert(p[0] == 4); |
| 242 |
assert(equal(p[1], "nrrnsnnni")); |
| 243 |
} |
| 244 |
|
| 245 |
@safe @nogc unittest |
| 246 |
{ |
| 247 |
import std.algorithm.comparison; |
| 248 |
|
| 249 |
int a = 5; |
| 250 |
short b = 6; |
| 251 |
double c = 2; |
| 252 |
auto d = max(a, b); |
| 253 |
assert(is(typeof(d) == int)); |
| 254 |
assert(d == 6); |
| 255 |
auto e = min(a, b, c); |
| 256 |
assert(is(typeof(e) == double)); |
| 257 |
assert(e == 2); |
| 258 |
} |
| 259 |
|
| 260 |
@safe @nogc unittest |
| 261 |
{ |
| 262 |
import std.algorithm.comparison; |
| 263 |
|
| 264 |
int a = 5; |
| 265 |
short b = 6; |
| 266 |
double c = 2; |
| 267 |
auto d = min(a, b); |
| 268 |
static assert(is(typeof(d) == int)); |
| 269 |
assert(d == 5); |
| 270 |
auto e = min(a, b, c); |
| 271 |
static assert(is(typeof(e) == double)); |
| 272 |
assert(e == 2); |
| 273 |
ulong f = 0xffff_ffff_ffff; |
| 274 |
const uint g = min(f, 0xffff_0000); |
| 275 |
assert(g == 0xffff_0000); |
| 276 |
dchar h = 100; |
| 277 |
uint i = 101; |
| 278 |
static assert(is(typeof(min(h, i)) == dchar)); |
| 279 |
static assert(is(typeof(min(i, h)) == uint)); |
| 280 |
assert(min(h, i) == 100); |
| 281 |
} |
| 282 |
|
| 283 |
@safe @nogc unittest |
| 284 |
{ |
| 285 |
import std.algorithm.comparison; |
| 286 |
|
| 287 |
int a = -10; |
| 288 |
uint f = 10; |
| 289 |
static assert(is(typeof(min(a, f)) == int)); |
| 290 |
assert(min(a, f) == -10); |
| 291 |
} |
| 292 |
|
| 293 |
@safe unittest |
| 294 |
{ |
| 295 |
import std.algorithm.comparison; |
| 296 |
|
| 297 |
import std.datetime; |
| 298 |
assert(min(Date(2012, 12, 21), Date(1982, 1, 4)) == Date(1982, 1, 4)); |
| 299 |
assert(min(Date(1982, 1, 4), Date(2012, 12, 21)) == Date(1982, 1, 4)); |
| 300 |
assert(min(Date(1982, 1, 4), Date.min) == Date.min); |
| 301 |
assert(min(Date.min, Date(1982, 1, 4)) == Date.min); |
| 302 |
assert(min(Date(1982, 1, 4), Date.max) == Date(1982, 1, 4)); |
| 303 |
assert(min(Date.max, Date(1982, 1, 4)) == Date(1982, 1, 4)); |
| 304 |
assert(min(Date.min, Date.max) == Date.min); |
| 305 |
assert(min(Date.max, Date.min) == Date.min); |
| 306 |
} |
| 307 |
|
| 308 |
@safe @nogc unittest |
| 309 |
{ |
| 310 |
import std.algorithm.comparison; |
| 311 |
|
| 312 |
int[6] x = [ 1, 5, 2, 7, 4, 3 ]; |
| 313 |
double[6] y = [ 1.0, 5, 2, 7.3, 4, 8 ]; |
| 314 |
auto m = mismatch(x[], y[]); |
| 315 |
assert(m[0] == x[3 .. $]); |
| 316 |
assert(m[1] == y[3 .. $]); |
| 317 |
|
| 318 |
auto m2 = mismatch(x[], y[], x[], y[]); |
| 319 |
assert(m2[0] == x[3 .. $]); |
| 320 |
assert(m2[1] == y[3 .. $]); |
| 321 |
assert(m2[2] == x[3 .. $]); |
| 322 |
assert(m2[3] == y[3 .. $]); |
| 323 |
} |
| 324 |
|
| 325 |
@safe unittest |
| 326 |
{ |
| 327 |
import std.algorithm.comparison; |
| 328 |
|
| 329 |
string res = 2.predSwitch!"a < b"( |
| 330 |
1, "less than 1", |
| 331 |
5, "less than 5", |
| 332 |
10, "less than 10", |
| 333 |
"greater or equal to 10"); |
| 334 |
|
| 335 |
assert(res == "less than 5"); |
| 336 |
|
| 337 |
//The arguments are lazy, which allows us to use predSwitch to create |
| 338 |
//recursive functions: |
| 339 |
int factorial(int n) |
| 340 |
{ |
| 341 |
return n.predSwitch!"a <= b"( |
| 342 |
-1, {throw new Exception("Can not calculate n! for n < 0");}(), |
| 343 |
0, 1, // 0! = 1 |
| 344 |
n * factorial(n - 1) // n! = n * (n - 1)! for n >= 0 |
| 345 |
); |
| 346 |
} |
| 347 |
assert(factorial(3) == 6); |
| 348 |
|
| 349 |
//Void return expressions are allowed if they always throw: |
| 350 |
import std.exception : assertThrown; |
| 351 |
assertThrown!Exception(factorial(-9)); |
| 352 |
} |
| 353 |
|
| 354 |
@safe nothrow pure unittest |
| 355 |
{ |
| 356 |
import std.algorithm.comparison; |
| 357 |
|
| 358 |
assert(isSameLength([1, 2, 3], [4, 5, 6])); |
| 359 |
assert(isSameLength([1, 2, 3], [4, 5, 6], [7, 8, 9])); |
| 360 |
assert(isSameLength([0.3, 90.4, 23.7, 119.2], [42.6, 23.6, 95.5, 6.3])); |
| 361 |
assert(isSameLength("abc", "xyz")); |
| 362 |
assert(isSameLength("abc", "xyz", [1, 2, 3])); |
| 363 |
|
| 364 |
int[] a; |
| 365 |
int[] b; |
| 366 |
assert(isSameLength(a, b)); |
| 367 |
assert(isSameLength(a, b, a, a, b, b, b)); |
| 368 |
|
| 369 |
assert(!isSameLength([1, 2, 3], [4, 5])); |
| 370 |
assert(!isSameLength([1, 2, 3], [4, 5, 6], [7, 8])); |
| 371 |
assert(!isSameLength([0.3, 90.4, 23.7], [42.6, 23.6, 95.5, 6.3])); |
| 372 |
assert(!isSameLength("abcd", "xyz")); |
| 373 |
assert(!isSameLength("abcd", "xyz", "123")); |
| 374 |
assert(!isSameLength("abcd", "xyz", "1234")); |
| 375 |
} |
| 376 |
|
| 377 |
@safe pure unittest |
| 378 |
{ |
| 379 |
import std.algorithm.comparison; |
| 380 |
|
| 381 |
import std.typecons : Yes; |
| 382 |
|
| 383 |
assert(isPermutation([1, 2, 3], [3, 2, 1])); |
| 384 |
assert(isPermutation([1.1, 2.3, 3.5], [2.3, 3.5, 1.1])); |
| 385 |
assert(isPermutation("abc", "bca")); |
| 386 |
|
| 387 |
assert(!isPermutation([1, 2], [3, 4])); |
| 388 |
assert(!isPermutation([1, 1, 2, 3], [1, 2, 2, 3])); |
| 389 |
assert(!isPermutation([1, 1], [1, 1, 1])); |
| 390 |
|
| 391 |
// Faster, but allocates GC handled memory |
| 392 |
assert(isPermutation!(Yes.allocateGC)([1.1, 2.3, 3.5], [2.3, 3.5, 1.1])); |
| 393 |
assert(!isPermutation!(Yes.allocateGC)([1, 2], [3, 4])); |
| 394 |
} |
| 395 |
|
| 396 |
@safe pure unittest |
| 397 |
{ |
| 398 |
import std.algorithm.comparison; |
| 399 |
|
| 400 |
const a = 1; |
| 401 |
const b = 2; |
| 402 |
auto ab = either(a, b); |
| 403 |
static assert(is(typeof(ab) == const(int))); |
| 404 |
assert(ab == a); |
| 405 |
|
| 406 |
auto c = 2; |
| 407 |
const d = 3; |
| 408 |
auto cd = either!(a => a == 3)(c, d); // use predicate |
| 409 |
static assert(is(typeof(cd) == int)); |
| 410 |
assert(cd == d); |
| 411 |
|
| 412 |
auto e = 0; |
| 413 |
const f = 2; |
| 414 |
auto ef = either(e, f); |
| 415 |
static assert(is(typeof(ef) == int)); |
| 416 |
assert(ef == f); |
| 417 |
} |
| 418 |
|
| 419 |
@safe pure unittest |
| 420 |
{ |
| 421 |
import std.algorithm.comparison; |
| 422 |
|
| 423 |
immutable p = 1; |
| 424 |
immutable q = 2; |
| 425 |
auto pq = either(p, q); |
| 426 |
static assert(is(typeof(pq) == immutable(int))); |
| 427 |
assert(pq == p); |
| 428 |
|
| 429 |
assert(either(3, 4) == 3); |
| 430 |
assert(either(0, 4) == 4); |
| 431 |
assert(either(0, 0) == 0); |
| 432 |
assert(either("", "a") == ""); |
| 433 |
} |
| 434 |
|
| 435 |
@safe pure unittest |
| 436 |
{ |
| 437 |
import std.algorithm.comparison; |
| 438 |
|
| 439 |
string r = null; |
| 440 |
assert(either(r, "a") == "a"); |
| 441 |
assert(either("a", "") == "a"); |
| 442 |
|
| 443 |
immutable s = [1, 2]; |
| 444 |
assert(either(s, s) == s); |
| 445 |
|
| 446 |
assert(either([0, 1], [1, 2]) == [0, 1]); |
| 447 |
assert(either([0, 1], [1]) == [0, 1]); |
| 448 |
assert(either("a", "b") == "a"); |
| 449 |
|
| 450 |
static assert(!__traits(compiles, either(1, "a"))); |
| 451 |
static assert(!__traits(compiles, either(1.0, "a"))); |
| 452 |
static assert(!__traits(compiles, either('a', "a"))); |
| 453 |
} |
| 454 |
|