diff --git a/source/tanya/container/hashtable.d b/source/tanya/container/hashtable.d index f1f8666..cf9f89b 100644 --- a/source/tanya/container/hashtable.d +++ b/source/tanya/container/hashtable.d @@ -20,7 +20,7 @@ import std.traits; import tanya.algorithm.mutation; import tanya.container.array; import tanya.container.entry; -import tanya.hash.lookup; +import tanya.math.hash; import tanya.memory.allocator; import tanya.memory.lifetime; import tanya.meta; diff --git a/source/tanya/container/set.d b/source/tanya/container/set.d index a8d9572..0c90b37 100644 --- a/source/tanya/container/set.d +++ b/source/tanya/container/set.d @@ -19,7 +19,7 @@ import std.range : isInfinite, isForwardRange; import std.traits; import tanya.container.array; import tanya.container.entry; -import tanya.hash.lookup; +import tanya.math.hash; import tanya.memory.allocator; import tanya.memory.lifetime; import tanya.meta; diff --git a/source/tanya/container/string.d b/source/tanya/container/string.d index 0401881..b94b275 100644 --- a/source/tanya/container/string.d +++ b/source/tanya/container/string.d @@ -31,7 +31,7 @@ import std.algorithm.mutation : bringToFront; import std.range : isInfinite, popFrontN, isInputRange; import std.traits; import tanya.algorithm.mutation; -import tanya.hash.lookup; +import tanya.math.hash; import tanya.memory.allocator; import tanya.memory.lifetime; import tanya.meta; diff --git a/source/tanya/hash/lookup.d b/source/tanya/hash/lookup.d deleted file mode 100644 index 2b5116d..0000000 --- a/source/tanya/hash/lookup.d +++ /dev/null @@ -1,342 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -/** - * Non-cryptographic, lookup hash functions. - * - * Copyright: Eugene Wissner 2018-2025. - * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/, - * Mozilla Public License, v. 2.0). - * Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner) - * Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/hash/lookup.d, - * tanya/hash/lookup.d) - */ -module tanya.hash.lookup; - -import std.range : isInfinite, isInputRange; -import std.traits; -import tanya.meta; -import tanya.range.primitive; - -private struct Hasher -{ - static if (size_t.sizeof == 4) - { - enum uint offsetBasis = 2166136261; - enum uint prime = 16777619; - } - else static if (size_t.sizeof == 8) - { - enum ulong offsetBasis = 14695981039346656037UL; - enum ulong prime = 1099511628211UL; - } - else static if (size_t.sizeof == 16) - { - enum size_t offsetBasis = (size_t(0x6c62272e07bb0142UL) << 64) + 0x62b821756295c58dUL; - enum size_t prime = (size_t(1) << 88) + (1 << 8) + 0x3b; - } - else - { - static assert(false, "FNV requires at least 32-bit hash length"); - } - - size_t hash = offsetBasis; - - void opCall(T)(auto ref T key) - { - static if (is(typeof(key.toHash()) == size_t)) - { - opCall(key.toHash()); // Combine user-defined hashes - } - else static if (isScalarType!T || isPointer!T) - { - // Treat as an array of words - static if (T.sizeof % size_t.sizeof == 0 - && T.alignof >= size_t.alignof) - alias CastT = size_t; - // (64-bit or 128-bit) Treat as an array of ints - else static if (T.sizeof % uint.sizeof == 0 - && T.alignof >= uint.alignof) - alias CastT = uint; - // Treat as an array of bytes - else - alias CastT = ubyte; - add((() @trusted => (cast(const CastT*) &key)[0 .. T.sizeof / CastT.sizeof])()); - } - else static if (isArray!T && isScalarType!(ElementType!T)) - { - // Treat as an array of words - static if (ElementType!T.sizeof % size_t.sizeof == 0 - && ElementType!T.alignof >= size_t.alignof) - alias CastT = size_t; - // (64-bit or 128-bit) Treat as an array of ints - else static if (ElementType!T.sizeof % uint.sizeof == 0 - && ElementType!T.alignof >= uint.alignof) - alias CastT = uint; - // Treat as an array of bytes - else - alias CastT = ubyte; - add(cast(const CastT[]) key); - } - else static if (is(T == typeof(null))) - { - add(key); - } - else static if (isInputRange!T && !isInfinite!T) - { - foreach (e; key) - { - opCall(e); - } - } - else - { - static assert(false, "Hash function is not available"); - } - } - - void add(scope const ubyte[] key) @nogc nothrow pure @safe - { - // FNV-1a - foreach (c; key) - { - this.hash = (this.hash ^ c) * prime; - } - } - - void add(scope const size_t[] key) @nogc nothrow pure @safe - { - static if (size_t.sizeof == 4) - { - // Partial MurmurHash3_x86_32 (no finalization) - enum uint c1 = 0xcc9e2d51; - enum uint c2 = 0x1b873593; - alias h1 = hash; - foreach (x; key) - { - auto k1 = x * c1; - k1 = (k1 << 15) | (k1 >> (32 - 15)); - k1 *= c2; - - h1 ^= k1; - h1 = (h1 << 13) | (h1 >> (32 - 13)); - h1 = h1 * 5 + 0xe6546b64; - } - } - else static if (size_t.sizeof == 8) - { - // Partial 64-bit MurmurHash64A (no finalization) - alias h = hash; - enum ulong m = 0xc6a4a7935bd1e995UL; - foreach (x; key) - { - auto k = x * m; - k ^= k >>> 47; - k *= m; - - h ^= k; - h *= m; - } - } - else static if (size_t.sizeof == 16) - { - // Partial MurmurHash3_x64_128 (no finalization) - // treating each size_t as a pair of ulong. - ulong h1 = cast(ulong) hash; - ulong h2 = cast(ulong) (hash >> 64); - - enum ulong c1 = 0x87c37b91114253d5UL; - enum ulong c2 = 0x4cf5ad432745937fUL; - - foreach (x; key) - { - auto k1 = cast(ulong) x; - auto k2 = cast(ulong) (x >> 64); - - k1 *= c1; k1 = (k1 << 32) | (k1 >> (64 - 31)); k1 *= c2; h1 ^= k1; - h1 = (h1 << 27) | (h1 >> (64 - 27)); h1 += h2; h1 = h1*5+0x52dce729; - k2 *= c2; k2 = (k2 << 33) | (k2 >> (64 - 33)); k2 *= c1; h2 ^= k2; - h2 = (h2 << 31) | (h2 >> (64 - 31)); h2 += h1; h2 = h2*5+0x38495ab5; - } - - hash = cast(size_t) h1 + ((cast(size_t) h2) << 64); - } - else - { - static assert(0, "Hash length must be either 32, 64, or 128 bits."); - } - } - - static if (size_t.sizeof != uint.sizeof) - void add(scope const uint[] key) @nogc nothrow pure @trusted - { - static if (size_t.sizeof == 8) - { - // Partial 32-bit MurmurHash64B (no finalization) - enum uint m = 0x5bd1e995; - enum r = 24; - - uint h1 = cast(uint) hash; - uint h2 = cast(uint) (hash >> 32); - const(uint)* data = key.ptr; - auto len = key.length; - - for (; len >= 2; data += 2, len -= 2) - { - uint k1 = data[0]; - k1 *= m; k1 ^= k1 >> r; k1 *= m; - h1 *= m; h1 ^= k1; - - uint k2 = data[1]; - k2 *= m; k2 ^= k2 >> r; k2 *= m; - h2 *= m; h2 ^= k2; - } - if (len) - { - uint k1 = data[0]; - k1 *= m; k1 ^= k1 >> r; k1 *= m; - h1 *= m; h1 ^= k1; - } - hash = cast(ulong) h1 + ((cast(ulong) h2) << 32); - } - else static if (size_t.sizeof == 16) - { - // Partial MurmurHash3_x86_128 (no finalization) - enum uint c1 = 0x239b961b; - enum uint c2 = 0xab0e9789; - enum uint c3 = 0x38b34ae5; - enum uint c4 = 0xa1e38b93; - - uint h1 = cast(uint) hash; - uint h2 = cast(uint) (hash >> 32); - uint h3 = cast(uint) (hash >> 64); - uint h4 = cast(uint) (hash >> 96); - const(uint)* data = key.ptr; - auto len = key.length; - - for (; len >= 4; data += 4, len -= 4) - { - uint k1 = data[0]; - uint k2 = data[1]; - uint k3 = data[2]; - uint k4 = data[3]; - - h1 = (h1 << 19) | (h1 >> (32 - 19)); h1 += h2; h1 = h1*5+0x561ccd1b; - k2 *= c2; k2 = (k2 << 16) | (k2 >> (32 - 16)); k2 *= c3; h2 ^= k2; - h2 = (h2 << 17) | (h2 >> (32 - 17)); h2 += h3; h2 = h2*5+0x0bcaa747; - k3 *= c3; k3 = (k3 << 17) | (k3 >> (32 - 17)); k3 *= c4; h3 ^= k3; - h3 = (h3 << 15) | (h3 >> (32 - 15)); h3 += h4; h3 = h3*5+0x96cd1c35; - k4 *= c4; k4 = (k4 << 18) | (k4 >> (32 - 18)); k4 *= c1; h4 ^= k4; - h4 = (h4 << 13) | (h4 >> (32 - 13)); h4 += h1; h4 = h4*5+0x32ac3b17; - } - uint k1, k2, k3; - switch (len) // 0, 1, 2, 3 - { - case 3: - k3 = data[2]; - k3 *= c3; k3 = (k3 << 17) | (k3 >> (32 - 17)); k3 *= c4; h3 ^= k3; - goto case; - case 2: - k2 = data[1]; - k2 *= c2; k2 = (k2 << 16) | (k2 >> (32 - 16)); k2 *= c3; h2 ^= k2; - goto case; - case 1: - k1 = data[0]; - k1 *= c1; k1 = (k1 << 15) | (k1 >> (32 - 15)); k1 *= c2; h1 ^= k1; - break; - } - hash = cast(size_t) h1 + - ((cast(size_t) h2) << 32) + - ((cast(size_t) h3) << 64) + - ((cast(size_t) h4) << 96); - } - else - { - static assert(0, "Hash length must be either 32, 64, or 128 bits."); - } - } -} - -/** - * Takes an argument of an arbitrary type $(D_PARAM T) and calculates the hash - * value. - * - * Hash calculation is supported for all scalar types. Aggregate types, like - * $(D_KEYWORD struct)s, should implement `toHash`-function: - * --- - * size_t toHash() const - * { - * return hash; - * } - * --- - * - * For pointers and for scalar types implicitly convertible to `size_t` this - * is an identity operation (i.e. the value is cast to `size_t` and returned - * unaltered). Integer types wider than `size_t` are XOR folded down to - * `size_t`. Other scalar types use an architecture-dependent hash function - * based on their width and alignment. - * If the type provides a `toHash`-function, only `toHash()` is called and its - * result is returned. - * - * This function also accepts input ranges that contain hashable elements. - * Individual values are combined then and the resulting hash is returned. - * - * Params: - * T = Hashable type. - * key = Hashable value. - * - * Returns: Calculated hash value. - * - * See_Also: $(LINK http://www.isthe.com/chongo/tech/comp/fnv/). - */ -size_t hash(T)(auto ref T key) -{ - static if (is(typeof(key.toHash()) == size_t)) - { - return key.toHash(); - } - else static if ((isIntegral!T || isSomeChar!T || isBoolean!T) - && T.sizeof <= size_t.sizeof) - { - return cast(size_t) key; - } - else static if (isIntegral!T && T.sizeof > size_t.sizeof) - { - return cast(size_t) (key ^ (key >>> (size_t.sizeof * 8))); - } - else static if (isPointer!T || is(T : typeof(null))) - { - return (() @trusted => cast(size_t) key)(); - } - else - { - Hasher hasher; - hasher(key); - return hasher.hash; - } -} - -/** - * Determines whether $(D_PARAM hasher) is hash function for $(D_PARAM T), i.e. - * it is callable with a value of type $(D_PARAM T) and returns a - * $(D_PSYMBOL size_t) value. - * - * Params: - * hasher = Hash function candidate. - * T = Type to test the hash function with. - * - * Returns: $(D_KEYWORD true) if $(D_PARAM hasher) is a hash function for - * $(D_PARAM T), $(D_KEYWORD false) otherwise. - */ -template isHashFunction(alias hasher, T) -{ - private alias wrapper = (T x) => hasher(x); - enum bool isHashFunction = is(typeof(wrapper(T.init)) == size_t); -} - -/// -@nogc nothrow pure @safe unittest -{ - static assert(isHashFunction!(hash, int)); -} diff --git a/source/tanya/hash/package.d b/source/tanya/hash/package.d deleted file mode 100644 index 89f6440..0000000 --- a/source/tanya/hash/package.d +++ /dev/null @@ -1,15 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -/** - * Copyright: Eugene Wissner 2018-2025. - * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/, - * Mozilla Public License, v. 2.0). - * Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner) - * Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/hash/package.d, - * tanya/hash/package.d) - */ -module tanya.hash; - -public import tanya.hash.lookup; diff --git a/tests/tanya/hash/tests/lookup.d b/source/tanya/math/hash.d similarity index 68% rename from tests/tanya/hash/tests/lookup.d rename to source/tanya/math/hash.d index 7d05099..feba9c9 100644 --- a/tests/tanya/hash/tests/lookup.d +++ b/source/tanya/math/hash.d @@ -1,10 +1,370 @@ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -module tanya.hash.tests.lookup; -import tanya.hash.lookup; -import tanya.test.stub; +/** + * Non-cryptographic, lookup hash functions. + * + * Copyright: Eugene Wissner 2018-2025. + * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/, + * Mozilla Public License, v. 2.0). + * Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner) + * Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/hash/lookup.d, + * tanya/hash/lookup.d) + */ +module tanya.math.hash; + +import std.range : isInfinite, isInputRange; +import std.traits; +import tanya.meta; +import tanya.range.primitive; + +version (unittest) { + import tanya.test.stub; + + private enum string r10(string x) = x ~ x ~ x ~ x ~ x ~ x ~ x ~ x ~ x ~ x; + private enum string r100(string x) = r10!x ~ r10!x ~ r10!x ~ r10!x ~ r10!x + ~ r10!x ~ r10!x ~ r10!x ~ r10!x ~ r10!x; + private enum string r500(string x) = r100!x ~ r100!x ~ r100!x ~ r100!x ~ r100!x; + + private struct HashRange + { + string fo = "fo"; + + @property ubyte front() const @nogc nothrow pure @safe + { + return this.fo[0]; + } + + void popFront() @nogc nothrow pure @safe + { + this.fo = this.fo[1 .. $]; + } + + @property bool empty() const @nogc nothrow pure @safe + { + return this.fo.length == 0; + } + } + + private struct ToHashRange + { + bool empty_; + + @property Hashable front() const @nogc nothrow pure @safe + { + return Hashable(); + } + + void popFront() @nogc nothrow pure @safe + { + this.empty_ = true; + } + + @property bool empty() const @nogc nothrow pure @safe + { + return this.empty_; + } + } +} + +private struct Hasher +{ + static if (size_t.sizeof == 4) + { + enum uint offsetBasis = 2166136261; + enum uint prime = 16777619; + } + else static if (size_t.sizeof == 8) + { + enum ulong offsetBasis = 14695981039346656037UL; + enum ulong prime = 1099511628211UL; + } + else static if (size_t.sizeof == 16) + { + enum size_t offsetBasis = (size_t(0x6c62272e07bb0142UL) << 64) + 0x62b821756295c58dUL; + enum size_t prime = (size_t(1) << 88) + (1 << 8) + 0x3b; + } + else + { + static assert(false, "FNV requires at least 32-bit hash length"); + } + + size_t hash = offsetBasis; + + void opCall(T)(auto ref T key) + { + static if (is(typeof(key.toHash()) == size_t)) + { + opCall(key.toHash()); // Combine user-defined hashes + } + else static if (isScalarType!T || isPointer!T) + { + // Treat as an array of words + static if (T.sizeof % size_t.sizeof == 0 + && T.alignof >= size_t.alignof) + alias CastT = size_t; + // (64-bit or 128-bit) Treat as an array of ints + else static if (T.sizeof % uint.sizeof == 0 + && T.alignof >= uint.alignof) + alias CastT = uint; + // Treat as an array of bytes + else + alias CastT = ubyte; + add((() @trusted => (cast(const CastT*) &key)[0 .. T.sizeof / CastT.sizeof])()); + } + else static if (isArray!T && isScalarType!(ElementType!T)) + { + // Treat as an array of words + static if (ElementType!T.sizeof % size_t.sizeof == 0 + && ElementType!T.alignof >= size_t.alignof) + alias CastT = size_t; + // (64-bit or 128-bit) Treat as an array of ints + else static if (ElementType!T.sizeof % uint.sizeof == 0 + && ElementType!T.alignof >= uint.alignof) + alias CastT = uint; + // Treat as an array of bytes + else + alias CastT = ubyte; + add(cast(const CastT[]) key); + } + else static if (is(T == typeof(null))) + { + add(key); + } + else static if (isInputRange!T && !isInfinite!T) + { + foreach (e; key) + { + opCall(e); + } + } + else + { + static assert(false, "Hash function is not available"); + } + } + + void add(scope const ubyte[] key) @nogc nothrow pure @safe + { + // FNV-1a + foreach (c; key) + { + this.hash = (this.hash ^ c) * prime; + } + } + + void add(scope const size_t[] key) @nogc nothrow pure @safe + { + static if (size_t.sizeof == 4) + { + // Partial MurmurHash3_x86_32 (no finalization) + enum uint c1 = 0xcc9e2d51; + enum uint c2 = 0x1b873593; + alias h1 = hash; + foreach (x; key) + { + auto k1 = x * c1; + k1 = (k1 << 15) | (k1 >> (32 - 15)); + k1 *= c2; + + h1 ^= k1; + h1 = (h1 << 13) | (h1 >> (32 - 13)); + h1 = h1 * 5 + 0xe6546b64; + } + } + else static if (size_t.sizeof == 8) + { + // Partial 64-bit MurmurHash64A (no finalization) + alias h = hash; + enum ulong m = 0xc6a4a7935bd1e995UL; + foreach (x; key) + { + auto k = x * m; + k ^= k >>> 47; + k *= m; + + h ^= k; + h *= m; + } + } + else static if (size_t.sizeof == 16) + { + // Partial MurmurHash3_x64_128 (no finalization) + // treating each size_t as a pair of ulong. + ulong h1 = cast(ulong) hash; + ulong h2 = cast(ulong) (hash >> 64); + + enum ulong c1 = 0x87c37b91114253d5UL; + enum ulong c2 = 0x4cf5ad432745937fUL; + + foreach (x; key) + { + auto k1 = cast(ulong) x; + auto k2 = cast(ulong) (x >> 64); + + k1 *= c1; k1 = (k1 << 32) | (k1 >> (64 - 31)); k1 *= c2; h1 ^= k1; + h1 = (h1 << 27) | (h1 >> (64 - 27)); h1 += h2; h1 = h1*5+0x52dce729; + k2 *= c2; k2 = (k2 << 33) | (k2 >> (64 - 33)); k2 *= c1; h2 ^= k2; + h2 = (h2 << 31) | (h2 >> (64 - 31)); h2 += h1; h2 = h2*5+0x38495ab5; + } + + hash = cast(size_t) h1 + ((cast(size_t) h2) << 64); + } + else + { + static assert(0, "Hash length must be either 32, 64, or 128 bits."); + } + } + + static if (size_t.sizeof != uint.sizeof) + void add(scope const uint[] key) @nogc nothrow pure @trusted + { + static if (size_t.sizeof == 8) + { + // Partial 32-bit MurmurHash64B (no finalization) + enum uint m = 0x5bd1e995; + enum r = 24; + + uint h1 = cast(uint) hash; + uint h2 = cast(uint) (hash >> 32); + const(uint)* data = key.ptr; + auto len = key.length; + + for (; len >= 2; data += 2, len -= 2) + { + uint k1 = data[0]; + k1 *= m; k1 ^= k1 >> r; k1 *= m; + h1 *= m; h1 ^= k1; + + uint k2 = data[1]; + k2 *= m; k2 ^= k2 >> r; k2 *= m; + h2 *= m; h2 ^= k2; + } + if (len) + { + uint k1 = data[0]; + k1 *= m; k1 ^= k1 >> r; k1 *= m; + h1 *= m; h1 ^= k1; + } + hash = cast(ulong) h1 + ((cast(ulong) h2) << 32); + } + else static if (size_t.sizeof == 16) + { + // Partial MurmurHash3_x86_128 (no finalization) + enum uint c1 = 0x239b961b; + enum uint c2 = 0xab0e9789; + enum uint c3 = 0x38b34ae5; + enum uint c4 = 0xa1e38b93; + + uint h1 = cast(uint) hash; + uint h2 = cast(uint) (hash >> 32); + uint h3 = cast(uint) (hash >> 64); + uint h4 = cast(uint) (hash >> 96); + const(uint)* data = key.ptr; + auto len = key.length; + + for (; len >= 4; data += 4, len -= 4) + { + uint k1 = data[0]; + uint k2 = data[1]; + uint k3 = data[2]; + uint k4 = data[3]; + + h1 = (h1 << 19) | (h1 >> (32 - 19)); h1 += h2; h1 = h1*5+0x561ccd1b; + k2 *= c2; k2 = (k2 << 16) | (k2 >> (32 - 16)); k2 *= c3; h2 ^= k2; + h2 = (h2 << 17) | (h2 >> (32 - 17)); h2 += h3; h2 = h2*5+0x0bcaa747; + k3 *= c3; k3 = (k3 << 17) | (k3 >> (32 - 17)); k3 *= c4; h3 ^= k3; + h3 = (h3 << 15) | (h3 >> (32 - 15)); h3 += h4; h3 = h3*5+0x96cd1c35; + k4 *= c4; k4 = (k4 << 18) | (k4 >> (32 - 18)); k4 *= c1; h4 ^= k4; + h4 = (h4 << 13) | (h4 >> (32 - 13)); h4 += h1; h4 = h4*5+0x32ac3b17; + } + uint k1, k2, k3; + switch (len) // 0, 1, 2, 3 + { + case 3: + k3 = data[2]; + k3 *= c3; k3 = (k3 << 17) | (k3 >> (32 - 17)); k3 *= c4; h3 ^= k3; + goto case; + case 2: + k2 = data[1]; + k2 *= c2; k2 = (k2 << 16) | (k2 >> (32 - 16)); k2 *= c3; h2 ^= k2; + goto case; + case 1: + k1 = data[0]; + k1 *= c1; k1 = (k1 << 15) | (k1 >> (32 - 15)); k1 *= c2; h1 ^= k1; + break; + } + hash = cast(size_t) h1 + + ((cast(size_t) h2) << 32) + + ((cast(size_t) h3) << 64) + + ((cast(size_t) h4) << 96); + } + else + { + static assert(0, "Hash length must be either 32, 64, or 128 bits."); + } + } +} + +/** + * Takes an argument of an arbitrary type $(D_PARAM T) and calculates the hash + * value. + * + * Hash calculation is supported for all scalar types. Aggregate types, like + * $(D_KEYWORD struct)s, should implement `toHash`-function: + * --- + * size_t toHash() const + * { + * return hash; + * } + * --- + * + * For pointers and for scalar types implicitly convertible to `size_t` this + * is an identity operation (i.e. the value is cast to `size_t` and returned + * unaltered). Integer types wider than `size_t` are XOR folded down to + * `size_t`. Other scalar types use an architecture-dependent hash function + * based on their width and alignment. + * If the type provides a `toHash`-function, only `toHash()` is called and its + * result is returned. + * + * This function also accepts input ranges that contain hashable elements. + * Individual values are combined then and the resulting hash is returned. + * + * Params: + * T = Hashable type. + * key = Hashable value. + * + * Returns: Calculated hash value. + * + * See_Also: $(LINK http://www.isthe.com/chongo/tech/comp/fnv/). + */ +size_t hash(T)(auto ref T key) +{ + static if (is(typeof(key.toHash()) == size_t)) + { + return key.toHash(); + } + else static if ((isIntegral!T || isSomeChar!T || isBoolean!T) + && T.sizeof <= size_t.sizeof) + { + return cast(size_t) key; + } + else static if (isIntegral!T && T.sizeof > size_t.sizeof) + { + return cast(size_t) (key ^ (key >>> (size_t.sizeof * 8))); + } + else static if (isPointer!T || is(T : typeof(null))) + { + return (() @trusted => cast(size_t) key)(); + } + else + { + Hasher hasher; + hasher(key); + return hasher.hash; + } +} // Tests that work for any hash size @nogc nothrow pure @safe unittest @@ -455,47 +815,26 @@ static if (size_t.sizeof == 8) @nogc nothrow pure @safe unittest assert(hash(r500!"\x7f") == 0x39e9f18f2f85e221UL); } -private enum string r10(string x) = x ~ x ~ x ~ x ~ x ~ x ~ x ~ x ~ x ~ x; -private enum string r100(string x) = r10!x ~ r10!x ~ r10!x ~ r10!x ~ r10!x - ~ r10!x ~ r10!x ~ r10!x ~ r10!x ~ r10!x; -private enum string r500(string x) = r100!x ~ r100!x ~ r100!x ~ r100!x ~ r100!x; - -private struct HashRange +/** + * Determines whether $(D_PARAM hasher) is hash function for $(D_PARAM T), i.e. + * it is callable with a value of type $(D_PARAM T) and returns a + * $(D_PSYMBOL size_t) value. + * + * Params: + * hasher = Hash function candidate. + * T = Type to test the hash function with. + * + * Returns: $(D_KEYWORD true) if $(D_PARAM hasher) is a hash function for + * $(D_PARAM T), $(D_KEYWORD false) otherwise. + */ +template isHashFunction(alias hasher, T) { - string fo = "fo"; - - @property ubyte front() const @nogc nothrow pure @safe - { - return this.fo[0]; - } - - void popFront() @nogc nothrow pure @safe - { - this.fo = this.fo[1 .. $]; - } - - @property bool empty() const @nogc nothrow pure @safe - { - return this.fo.length == 0; - } + private alias wrapper = (T x) => hasher(x); + enum bool isHashFunction = is(typeof(wrapper(T.init)) == size_t); } -private struct ToHashRange +/// +@nogc nothrow pure @safe unittest { - bool empty_; - - @property Hashable front() const @nogc nothrow pure @safe - { - return Hashable(); - } - - void popFront() @nogc nothrow pure @safe - { - this.empty_ = true; - } - - @property bool empty() const @nogc nothrow pure @safe - { - return this.empty_; - } + static assert(isHashFunction!(hash, int)); } diff --git a/source/tanya/math/random.d b/source/tanya/math/random.d index 6146788..41b2806 100644 --- a/source/tanya/math/random.d +++ b/source/tanya/math/random.d @@ -338,3 +338,13 @@ else version (Windows) } } } + +static if (is(PlatformEntropySource)) @nogc @system unittest +{ + import tanya.memory.smartref : unique; + + auto source = defaultAllocator.unique!PlatformEntropySource(); + + assert(source.threshold == 32); + assert(source.strong); +} diff --git a/source/tanya/net/iface.d b/source/tanya/net/iface.d index 6c5a962..55169d2 100644 --- a/source/tanya/net/iface.d +++ b/source/tanya/net/iface.d @@ -158,6 +158,26 @@ String indexToName(uint index) @nogc nothrow @trusted } } +@nogc nothrow @safe unittest +{ + import std.algorithm.comparison; + import std.utf; + + version (linux) + { + assert(equal(indexToName(1)[], "lo".byChar)); + } + else version (Windows) + { + assert(equal(indexToName(1)[], "loopback_0")); + } + else + { + assert(equal(indexToName(1)[], "lo0")); + } + assert(indexToName(uint.max).empty); +} + /** * $(D_PSYMBOL AddressFamily) specifies a communication domain; this selects * the protocol family which will be used for communication. diff --git a/source/tanya/net/inet.d b/source/tanya/net/inet.d index a919c9d..eb2b08b 100644 --- a/source/tanya/net/inet.d +++ b/source/tanya/net/inet.d @@ -190,6 +190,17 @@ if (L > ubyte.sizeof && L <= ulong.sizeof) assert(networkOrder.empty); } +// Static tests +@nogc nothrow pure @safe unittest +{ + import std.range; + + static assert(isBidirectionalRange!(NetworkOrder!4)); + static assert(isBidirectionalRange!(NetworkOrder!8)); + static assert(!is(NetworkOrder!9)); + static assert(!is(NetworkOrder!1)); +} + /** * Converts the $(D_KEYWORD ubyte) input range $(D_PARAM range) to * $(D_PARAM T). diff --git a/source/tanya/net/ip.d b/source/tanya/net/ip.d index 4baa6e2..7e464b1 100644 --- a/source/tanya/net/ip.d +++ b/source/tanya/net/ip.d @@ -319,6 +319,15 @@ struct Address4 } } +// Assignment and comparison works +@nogc nothrow pure @safe unittest +{ + auto address1 = Address4.loopback(); + auto address2 = Address4.any(); + address1 = address2; + assert(address1 == address2); +} + /** * Parses a string containing an IPv4 address in dotted-decimal notation. * @@ -417,6 +426,22 @@ if (isInputRange!R && is(Unqual!(ElementType!R) == ubyte)) } } +// Rejects malformed addresses +@nogc nothrow pure @safe unittest +{ + assert(address4("256.0.0.1").isNull); + assert(address4(".0.0.1").isNull); + assert(address4("0..0.1").isNull); + assert(address4("0.0.0.").isNull); + assert(address4("0.0.").isNull); + assert(address4("").isNull); +} + +@nogc nothrow pure @safe unittest +{ + assert(address4(cast(ubyte[]) []).isNull); +} + /** * IPv6 internet address. */ @@ -748,6 +773,15 @@ struct Address6 } } +// Can assign another address +@nogc nothrow pure @safe unittest +{ + Address actual = Address4.loopback; + Address expected = Address6.loopback; + actual = expected; + assert(actual == expected); +} + private void read2Bytes(R)(ref R range, ubyte[] address) { ushort group = readIntegral!ushort(range, 16); @@ -1057,6 +1091,119 @@ if (isInputRange!R && is(Unqual!(ElementType!R) == ubyte)) } } +@nogc nothrow @safe unittest +{ + char[18] actual; + + address6("ff00:2:3:4:5:6:7:8").get.toString(arrayInserter(actual)); + assert(actual[] == "ff00:2:3:4:5:6:7:8"); +} + +// Skips zero group in the middle +@nogc nothrow @safe unittest +{ + char[12] actual; + + address6("1::4:5:6:7:8").get.toString(arrayInserter(actual)); + assert(actual[] == "1::4:5:6:7:8"); +} + +// Doesn't replace lonely zeroes +@nogc nothrow @safe unittest +{ + char[15] actual; + + address6("0:1:0:2:3:0:4:0").get.toString(arrayInserter(actual)); + assert(actual[] == "0:1:0:2:3:0:4:0"); +} + +// Skips zero group at the beginning +@nogc nothrow @safe unittest +{ + char[13] actual; + + address6("::3:4:5:6:7:8").get.toString(arrayInserter(actual)); + assert(actual[] == "::3:4:5:6:7:8"); +} + +// Skips zero group at the end +@nogc nothrow @safe unittest +{ + char[13] actual; + + address6("1:2:3:4:5:6::").get.toString(arrayInserter(actual)); + assert(actual[] == "1:2:3:4:5:6::"); +} + +@nogc nothrow @safe unittest +{ + ubyte[16] expected = [0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8]; + auto actual = address6("1:2:3:4:5:6:7:8"); + assert(actual.get.toBytes() == expected); +} + +@nogc nothrow @safe unittest +{ + ubyte[16] expected; + auto actual = address6("::"); + assert(actual.get.toBytes() == expected); +} + +@nogc nothrow @safe unittest +{ + ubyte[16] expected = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]; + auto actual = address6("::1"); + assert(actual.get.toBytes() == expected); +} + +@nogc nothrow @safe unittest +{ + ubyte[16] expected = [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + auto actual = address6("1::"); + assert(actual.get.toBytes() == expected); +} + +// Rejects malformed addresses +@nogc nothrow @safe unittest +{ + assert(address6("").isNull); + assert(address6(":").isNull); + assert(address6(":a").isNull); + assert(address6("a:").isNull); + assert(address6("1:2:3:4::6:").isNull); + assert(address6("fe80:2:3:4::6:7:8%").isNull); +} + +// Parses embedded IPv4 address +@nogc nothrow @safe unittest +{ + ubyte[16] expected = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4]; + auto actual = address6("0:0:0:0:0:0:1.2.3.4"); + assert(actual.get.toBytes() == expected); +} + +@nogc nothrow @safe unittest +{ + ubyte[16] expected = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4]; + auto actual = address6("::1.2.3.4"); + assert(actual.get.toBytes() == expected); +} + +@nogc nothrow @safe unittest +{ + ubyte[16] expected = [0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 6, 1, 2, 3, 4]; + auto actual = address6("::5:6:1.2.3.4"); + assert(actual.get.toBytes() == expected); +} + +@nogc nothrow @safe unittest +{ + assert(address6("0:0:0:0:0:0:1.2.3.").isNull); + assert(address6("0:0:0:0:0:0:1.2:3.4").isNull); + assert(address6("0:0:0:0:0:0:1.2.3.4.").isNull); + assert(address6("fe80:0:0:0:0:0:1.2.3.4%1").get.scopeID == 1); +} + /** * Address storage, that can hold either an IPv4 or IPv6 address. */ diff --git a/source/tanya/net/package.d b/source/tanya/net/package.d index f8762a7..f20e069 100644 --- a/source/tanya/net/package.d +++ b/source/tanya/net/package.d @@ -17,4 +17,3 @@ module tanya.net; public import tanya.net.iface; public import tanya.net.inet; public import tanya.net.ip; -public import tanya.net.uri; diff --git a/source/tanya/net/uri.d b/source/tanya/net/uri.d deleted file mode 100644 index d164724..0000000 --- a/source/tanya/net/uri.d +++ /dev/null @@ -1,411 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -/** - * URL parser. - * - * Copyright: Eugene Wissner 2017-2025. - * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/, - * Mozilla Public License, v. 2.0). - * Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner) - * Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/net/uri.d, - * tanya/net/uri.d) - */ -module tanya.net.uri; - -import std.ascii; -import tanya.conv; -import tanya.memory.allocator; - -/** - * Thrown if an invalid URI was specified. - */ -final class URIException : Exception -{ - /** - * Params: - * msg = The message for the exception. - * file = The file where the exception occurred. - * line = The line number where the exception occurred. - * next = The previous exception in the chain of exceptions, if any. - */ - this(string msg, - string file = __FILE__, - size_t line = __LINE__, - Throwable next = null) @nogc nothrow pure @safe - { - super(msg, file, line, next); - } -} - -/** - * A Unique Resource Locator. - */ -struct URL -{ - /// The URL scheme. - const(char)[] scheme; - - /// The username. - const(char)[] user; - - /// The password. - const(char)[] pass; - - /// The hostname. - const(char)[] host; - - /// The port number. - ushort port; - - /// The path. - const(char)[] path; - - /// The query string. - const(char)[] query; - - /// The anchor. - const(char)[] fragment; - - /** - * Attempts to parse an URL from a string. - * Output string data (scheme, user, etc.) are just slices of input string - * (i.e., no memory allocation and copying). - * - * Params: - * source = The string containing the URL. - * - * Throws: $(D_PSYMBOL URIException) if the URL is malformed. - */ - this(const char[] source) @nogc pure - { - ptrdiff_t pos = -1, endPos = source.length, start; - - foreach (i, ref c; source) - { - if (pos == -1 && c == ':') - { - pos = i; - } - if (endPos == source.length && (c == '?' || c == '#')) - { - endPos = i; - } - } - - // Check if the colon is a part of the scheme or the port and parse - // the appropriate part. - if (source.length > 1 && source[0] == '/' && source[1] == '/') - { - // Relative scheme. - start = 2; - } - else if (pos > 0) - { - // Validate scheme: - // [ toLower(alpha) | digit | "+" | "-" | "." ] - foreach (ref c; source[0 .. pos]) - { - if (!c.isAlphaNum && c != '+' && c != '-' && c != '.') - { - goto ParsePath; - } - } - - if (source.length == pos + 1) // only "scheme:" is available. - { - this.scheme = source[0 .. $ - 1]; - return; - } - else if (source.length > pos + 1 && source[pos + 1] == '/') - { - this.scheme = source[0 .. pos]; - - if (source.length > pos + 2 && source[pos + 2] == '/') - { - start = pos + 3; - - if (source.length <= start) - { - // Only "scheme://" is available. - return; - } - if (this.scheme == "file" && source[start] == '/') - { - // Windows drive letters. - if (source.length - start > 2 - && source[start + 2] == ':') - { - ++start; - } - goto ParsePath; - } - } - else - { - start = pos + 1; - goto ParsePath; - } - } - else if (!parsePort(source[pos .. $])) - { - // Schemas like mailto: and zlib: may not have any slash after - // them. - this.scheme = source[0 .. pos]; - start = pos + 1; - goto ParsePath; - } - } - else if (pos == 0 && parsePort(source[pos .. $])) - { - // An URL shouldn't begin with a port number. - throw defaultAllocator.make!URIException("URL begins with port"); - } - else - { - goto ParsePath; - } - - // Parse host. - pos = -1; - for (ptrdiff_t i = start; i < source.length; ++i) - { - if (source[i] == '@') - { - pos = i; - } - else if (source[i] == '/') - { - endPos = i; - break; - } - } - - // Check for login and password. - if (pos != -1) - { - // *( unreserved / pct-encoded / sub-delims / ":" ) - foreach (i, c; source[start .. pos]) - { - if (c == ':') - { - if (this.user is null) - { - this.user = source[start .. start + i]; - this.pass = source[start + i + 1 .. pos]; - } - } - else if (!c.isAlpha() && - !c.isDigit() && - c != '!' && - c != ';' && - c != '=' && - c != '_' && - c != '~' && - !(c >= '$' && c <= '.')) - { - this.scheme = this.user = this.pass = null; - throw make!URIException(defaultAllocator, - "Restricted characters in user information"); - } - } - if (this.user is null) - { - this.user = source[start .. pos]; - } - - start = ++pos; - } - - pos = endPos; - if (endPos <= 1 || source[start] != '[' || source[endPos - 1] != ']') - { - // Short circuit portscan. - // IPv6 embedded address. - for (ptrdiff_t i = endPos - 1; i >= start; --i) - { - if (source[i] == ':') - { - pos = i; - if (this.port == 0 && !parsePort(source[i .. endPos])) - { - this.scheme = this.user = this.pass = null; - throw defaultAllocator.make!URIException("Invalid port"); - } - break; - } - } - } - - // Check if we have a valid host, if we don't reject the string as URL. - if (pos <= start) - { - this.scheme = this.user = this.pass = null; - throw defaultAllocator.make!URIException("Invalid host"); - } - - this.host = source[start .. pos]; - - if (endPos == source.length) - { - return; - } - - start = endPos; - - ParsePath: - endPos = source.length; - pos = -1; - foreach (i, ref c; source[start .. $]) - { - if (c == '?' && pos == -1) - { - pos = start + i; - } - else if (c == '#') - { - endPos = start + i; - break; - } - } - if (pos == -1) - { - pos = endPos; - } - - if (pos > start) - { - this.path = source[start .. pos]; - } - if (endPos >= ++pos) - { - this.query = source[pos .. endPos]; - } - if (++endPos <= source.length) - { - this.fragment = source[endPos .. $]; - } - } - - /* - * Attempts to parse and set the port. - * - * Params: - * port = String beginning with a colon followed by the port number and - * an optional path (query string and/or fragment), like: - * `:12345/some_path` or `:12345`. - * - * Returns: Whether the port could be found. - */ - private bool parsePort(const(char)[] port) @nogc nothrow pure @safe - { - auto unparsed = port[1 .. $]; - auto parsed = readIntegral!ushort(unparsed); - if (unparsed.length == 0 || unparsed[0] == '/') - { - this.port = parsed; - return true; - } - return false; - } -} - -/// -@nogc pure @system unittest -{ - auto u = URL("example.org"); - assert(u.path == "example.org"); - - u = URL("relative/path"); - assert(u.path == "relative/path"); - - // Host and scheme - u = URL("https://example.org"); - assert(u.scheme == "https"); - assert(u.host == "example.org"); - assert(u.path is null); - assert(u.port == 0); - assert(u.fragment is null); - - // With user and port and path - u = URL("https://hilary:putnam@example.org:443/foo/bar"); - assert(u.scheme == "https"); - assert(u.host == "example.org"); - assert(u.path == "/foo/bar"); - assert(u.port == 443); - assert(u.user == "hilary"); - assert(u.pass == "putnam"); - assert(u.fragment is null); - - // With query string - u = URL("https://example.org/?login=true"); - assert(u.scheme == "https"); - assert(u.host == "example.org"); - assert(u.path == "/"); - assert(u.query == "login=true"); - assert(u.fragment is null); - - // With query string and fragment - u = URL("https://example.org/?login=false#label"); - assert(u.scheme == "https"); - assert(u.host == "example.org"); - assert(u.path == "/"); - assert(u.query == "login=false"); - assert(u.fragment == "label"); - - u = URL("redis://root:password@localhost:2201/path?query=value#fragment"); - assert(u.scheme == "redis"); - assert(u.user == "root"); - assert(u.pass == "password"); - assert(u.host == "localhost"); - assert(u.port == 2201); - assert(u.path == "/path"); - assert(u.query == "query=value"); - assert(u.fragment == "fragment"); -} - -/** - * Attempts to parse an URL from a string and returns the specified component - * of the URL or $(D_PSYMBOL URL) if no component is specified. - * - * Params: - * T = "scheme", "host", "port", "user", "pass", "path", "query", - * "fragment". - * source = The string containing the URL. - * - * Returns: Requested URL component. - */ -auto parseURL(string T)(const char[] source) -if (T == "scheme" - || T == "host" - || T == "user" - || T == "pass" - || T == "path" - || T == "query" - || T == "fragment" - || T == "port") -{ - auto ret = URL(source); - return mixin("ret." ~ T); -} - -/// ditto -URL parseURL(const char[] source) @nogc pure -{ - return URL(source); -} - -/// -@nogc pure @system unittest -{ - auto u = parseURL("http://example.org:5326"); - assert(u.scheme == parseURL!"scheme"("http://example.org:5326")); - assert(u.host == parseURL!"host"("http://example.org:5326")); - assert(u.user == parseURL!"user"("http://example.org:5326")); - assert(u.pass == parseURL!"pass"("http://example.org:5326")); - assert(u.path == parseURL!"path"("http://example.org:5326")); - assert(u.query == parseURL!"query"("http://example.org:5326")); - assert(u.fragment == parseURL!"fragment"("http://example.org:5326")); - assert(u.port == parseURL!"port"("http://example.org:5326")); -} diff --git a/source/tanya/range/primitive.d b/source/tanya/range/primitive.d index ec1eb0b..935e887 100644 --- a/source/tanya/range/primitive.d +++ b/source/tanya/range/primitive.d @@ -140,44 +140,6 @@ private struct Primitive(Candidate, string primitive) } } -/** - * Determines whether `r1.front` and `r2.front` point to the same element. - * - * Params: - * r1 = First range. - * r2 = Second range. - * - * Returns: $(D_KEYWORD true) if $(D_PARAM r1) and $(D_PARAM r2) have the same - * head, $(D_KEYWORD false) otherwise. - */ -bool sameHead(Range)(Range r1, Range r2) @trusted -if (isInputRange!Range && hasLvalueElements!Range) -{ - return &r1.front is &r2.front; -} - -/// -@nogc nothrow pure @safe unittest -{ - const int[2] array; - - auto r1 = array[]; - auto r2 = array[]; - - assert(sameHead(r1, r2)); -} - -/// -@nogc nothrow pure @safe unittest -{ - const int[2] array; - - auto r1 = array[]; - auto r2 = array[1 .. $]; - - assert(!sameHead(r1, r2)); -} - /** * Returns the first element and advances the range. * diff --git a/tests/tanya/math/tests/package.d b/tests/tanya/math/tests/package.d deleted file mode 100644 index 2fc47ef..0000000 --- a/tests/tanya/math/tests/package.d +++ /dev/null @@ -1,6 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -module tanya.math.tests; - -import tanya.math; diff --git a/tests/tanya/math/tests/random.d b/tests/tanya/math/tests/random.d deleted file mode 100644 index 2fe9f91..0000000 --- a/tests/tanya/math/tests/random.d +++ /dev/null @@ -1,17 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -module tanya.math.tests.random; - -import tanya.math.random; -import tanya.memory.allocator; - -static if (is(PlatformEntropySource)) @nogc @system unittest -{ - import tanya.memory.smartref : unique; - - auto source = defaultAllocator.unique!PlatformEntropySource(); - - assert(source.threshold == 32); - assert(source.strong); -} diff --git a/tests/tanya/net/tests/iface.d b/tests/tanya/net/tests/iface.d deleted file mode 100644 index 7726b0e..0000000 --- a/tests/tanya/net/tests/iface.d +++ /dev/null @@ -1,25 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -module tanya.net.tests.iface; - -import std.algorithm.comparison; -import std.utf; -import tanya.net.iface; - -@nogc nothrow @safe unittest -{ - version (linux) - { - assert(equal(indexToName(1)[], "lo".byChar)); - } - else version (Windows) - { - assert(equal(indexToName(1)[], "loopback_0")); - } - else - { - assert(equal(indexToName(1)[], "lo0")); - } - assert(indexToName(uint.max).empty); -} diff --git a/tests/tanya/net/tests/inet.d b/tests/tanya/net/tests/inet.d deleted file mode 100644 index 61b6ecc..0000000 --- a/tests/tanya/net/tests/inet.d +++ /dev/null @@ -1,16 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -module tanya.net.tests.inet; - -import tanya.net.inet; -import std.range; - -// Static tests -@nogc nothrow pure @safe unittest -{ - static assert(isBidirectionalRange!(NetworkOrder!4)); - static assert(isBidirectionalRange!(NetworkOrder!8)); - static assert(!is(NetworkOrder!9)); - static assert(!is(NetworkOrder!1)); -} diff --git a/tests/tanya/net/tests/ip.d b/tests/tanya/net/tests/ip.d deleted file mode 100644 index 3c3dc0c..0000000 --- a/tests/tanya/net/tests/ip.d +++ /dev/null @@ -1,154 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -module tanya.net.tests.ip; - -import tanya.net.ip; -import tanya.range; - -// Rejects malformed addresses -@nogc nothrow pure @safe unittest -{ - assert(address4("256.0.0.1").isNull); - assert(address4(".0.0.1").isNull); - assert(address4("0..0.1").isNull); - assert(address4("0.0.0.").isNull); - assert(address4("0.0.").isNull); - assert(address4("").isNull); -} - -@nogc nothrow pure @safe unittest -{ - assert(address4(cast(ubyte[]) []).isNull); -} - -// Assignment and comparison works -@nogc nothrow pure @safe unittest -{ - auto address1 = Address4.loopback(); - auto address2 = Address4.any(); - address1 = address2; - assert(address1 == address2); -} - -@nogc nothrow @safe unittest -{ - char[18] actual; - - address6("ff00:2:3:4:5:6:7:8").get.toString(arrayInserter(actual)); - assert(actual[] == "ff00:2:3:4:5:6:7:8"); -} - -// Skips zero group in the middle -@nogc nothrow @safe unittest -{ - char[12] actual; - - address6("1::4:5:6:7:8").get.toString(arrayInserter(actual)); - assert(actual[] == "1::4:5:6:7:8"); -} - -// Doesn't replace lonely zeroes -@nogc nothrow @safe unittest -{ - char[15] actual; - - address6("0:1:0:2:3:0:4:0").get.toString(arrayInserter(actual)); - assert(actual[] == "0:1:0:2:3:0:4:0"); -} - -// Skips zero group at the beginning -@nogc nothrow @safe unittest -{ - char[13] actual; - - address6("::3:4:5:6:7:8").get.toString(arrayInserter(actual)); - assert(actual[] == "::3:4:5:6:7:8"); -} - -// Skips zero group at the end -@nogc nothrow @safe unittest -{ - char[13] actual; - - address6("1:2:3:4:5:6::").get.toString(arrayInserter(actual)); - assert(actual[] == "1:2:3:4:5:6::"); -} - -@nogc nothrow @safe unittest -{ - ubyte[16] expected = [0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8]; - auto actual = address6("1:2:3:4:5:6:7:8"); - assert(actual.get.toBytes() == expected); -} - -@nogc nothrow @safe unittest -{ - ubyte[16] expected; - auto actual = address6("::"); - assert(actual.get.toBytes() == expected); -} - -@nogc nothrow @safe unittest -{ - ubyte[16] expected = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]; - auto actual = address6("::1"); - assert(actual.get.toBytes() == expected); -} - -@nogc nothrow @safe unittest -{ - ubyte[16] expected = [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; - auto actual = address6("1::"); - assert(actual.get.toBytes() == expected); -} - -// Rejects malformed addresses -@nogc nothrow @safe unittest -{ - assert(address6("").isNull); - assert(address6(":").isNull); - assert(address6(":a").isNull); - assert(address6("a:").isNull); - assert(address6("1:2:3:4::6:").isNull); - assert(address6("fe80:2:3:4::6:7:8%").isNull); -} - -// Parses embedded IPv4 address -@nogc nothrow @safe unittest -{ - ubyte[16] expected = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4]; - auto actual = address6("0:0:0:0:0:0:1.2.3.4"); - assert(actual.get.toBytes() == expected); -} - -@nogc nothrow @safe unittest -{ - ubyte[16] expected = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4]; - auto actual = address6("::1.2.3.4"); - assert(actual.get.toBytes() == expected); -} - -@nogc nothrow @safe unittest -{ - ubyte[16] expected = [0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 6, 1, 2, 3, 4]; - auto actual = address6("::5:6:1.2.3.4"); - assert(actual.get.toBytes() == expected); -} - -@nogc nothrow @safe unittest -{ - assert(address6("0:0:0:0:0:0:1.2.3.").isNull); - assert(address6("0:0:0:0:0:0:1.2:3.4").isNull); - assert(address6("0:0:0:0:0:0:1.2.3.4.").isNull); - assert(address6("fe80:0:0:0:0:0:1.2.3.4%1").get.scopeID == 1); -} - -// Can assign another address -@nogc nothrow pure @safe unittest -{ - Address actual = Address4.loopback; - Address expected = Address6.loopback; - actual = expected; - assert(actual == expected); -} diff --git a/tests/tanya/net/tests/uri.d b/tests/tanya/net/tests/uri.d deleted file mode 100644 index 52d639a..0000000 --- a/tests/tanya/net/tests/uri.d +++ /dev/null @@ -1,136 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -module tanya.net.tests.uri; - -import tanya.net.uri; -import tanya.test.assertion; - -@nogc pure @system unittest -{ - const u = URL("127.0.0.1"); - assert(u.path == "127.0.0.1"); -} - -@nogc pure @system unittest -{ - const u = URL("http://127.0.0.1"); - assert(u.scheme == "http"); - assert(u.host == "127.0.0.1"); -} - -@nogc pure @system unittest -{ - const u = URL("http://127.0.0.1:9000"); - assert(u.scheme == "http"); - assert(u.host == "127.0.0.1"); - assert(u.port == 9000); -} - -@nogc pure @system unittest -{ - const u = URL("127.0.0.1:80"); - assert(u.host == "127.0.0.1"); - assert(u.port == 80); - assert(u.path is null); -} - -@nogc pure @system unittest -{ - const u = URL("//example.net"); - assert(u.host == "example.net"); - assert(u.scheme is null); -} - -@nogc pure @system unittest -{ - const u = URL("//example.net?q=before:after"); - assert(u.host == "example.net"); - assert(u.query == "q=before:after"); -} - -@nogc pure @system unittest -{ - const u = URL("localhost:8080"); - assert(u.host == "localhost"); - assert(u.port == 8080); - assert(u.path is null); -} - -@nogc pure @system unittest -{ - const u = URL("ftp:"); - assert(u.scheme == "ftp"); -} - -@nogc pure @system unittest -{ - const u = URL("file:///C:\\Users"); - assert(u.scheme == "file"); - assert(u.path == "C:\\Users"); -} - -@nogc pure @system unittest -{ - const u = URL("localhost:66000"); - assert(u.scheme == "localhost"); - assert(u.path == "66000"); -} - -@nogc pure @system unittest -{ - const u = URL("file:///home/"); - assert(u.scheme == "file"); - assert(u.path == "/home/"); -} - -@nogc pure @system unittest -{ - const u = URL("file:///home/?q=asdf"); - assert(u.scheme == "file"); - assert(u.path == "/home/"); - assert(u.query == "q=asdf"); -} - -@nogc pure @system unittest -{ - const u = URL("http://secret@example.org"); - assert(u.scheme == "http"); - assert(u.host == "example.org"); - assert(u.user == "secret"); -} - -@nogc pure @system unittest -{ - const u = URL("h_tp://:80"); - assert(u.path == "h_tp://:80"); - assert(u.port == 0); -} - -@nogc pure @system unittest -{ - const u = URL("zlib:/home/user/file.gz"); - assert(u.scheme == "zlib"); - assert(u.path == "/home/user/file.gz"); -} - -@nogc pure @system unittest -{ - const u = URL("h_tp:asdf"); - assert(u.path == "h_tp:asdf"); -} - -@nogc pure @system unittest -{ - assertThrown!URIException(() => URL("http://:80")); - assertThrown!URIException(() => URL(":80")); - assertThrown!URIException(() => URL("http://u1:p1@u2:p2@example.org")); - assertThrown!URIException(() => URL("http://blah.com:port")); - assertThrown!URIException(() => URL("http://blah.com:66000")); -} - -@nogc pure @system unittest -{ - const u = URL("ftp://"); - assert(u.scheme == "ftp"); -}