Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 8 additions & 15 deletions std/concurrency.d
Original file line number Diff line number Diff line change
Expand Up @@ -2430,12 +2430,11 @@ private
}
}

version(unittest)
@system unittest
{
import std.stdio;
import std.typecons : tuple, Tuple;

void testfn(Tid tid)
static void testfn(Tid tid)
{
receive((float val) { assert(0); }, (int val, int val2) {
assert(val == 42 && val2 == 86);
Expand All @@ -2450,7 +2449,7 @@ version(unittest)
prioritySend(tid, "done");
}

void runTest(Tid tid)
static void runTest(Tid tid)
{
send(tid, 42, 86);
send(tid, tuple(42, 86));
Expand All @@ -2459,7 +2458,7 @@ version(unittest)
receive((string val) { assert(val == "done"); });
}

void simpleTest()
static void simpleTest()
{
auto tid = spawn(&testfn, thisTid);
runTest(tid);
Expand All @@ -2470,17 +2469,11 @@ version(unittest)
runTest(tid);
}

@system unittest
{
simpleTest();
}
simpleTest();

@system unittest
{
scheduler = new ThreadScheduler;
simpleTest();
scheduler = null;
}
scheduler = new ThreadScheduler;
simpleTest();
scheduler = null;
}

private @property shared(Mutex) initOnceLock()
Expand Down
18 changes: 10 additions & 8 deletions std/conv.d
Original file line number Diff line number Diff line change
Expand Up @@ -5337,6 +5337,7 @@ version(unittest) private class __conv_EmplaceTestClass
assert(s.i == 2);
}
}

version(unittest)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since in-function structs depend on the order defined, these have to be defined at module-level. Did not work if I put it inside the unittest itself.

But these structs are super-simple, and have no external dependencies.

{
//Ambiguity
Expand All @@ -5357,16 +5358,17 @@ version(unittest)
ref __std_conv_S foo() return @property {s.i = j; return s;}
alias foo this;
}
}

@system unittest
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a fan of unittests being inside version(unittest), that's why the indentation changed.

{
static assert(is(__std_conv_SS : __std_conv_S));
@system unittest
{
__std_conv_S s = void;
__std_conv_SS ss = __std_conv_SS(1);
__std_conv_S s = void;
__std_conv_SS ss = __std_conv_SS(1);

__std_conv_S sTest1 = ss; //this calls "SS alias this" (and not "S.this(SS)")
emplace(&s, ss); //"alias this" should take precedence in emplace over "opCall"
assert(s.i == 1);
}
__std_conv_S sTest1 = ss; //this calls "SS alias this" (and not "S.this(SS)")
emplace(&s, ss); //"alias this" should take precedence in emplace over "opCall"
assert(s.i == 1);
}

//Nested classes
Expand Down
4 changes: 1 addition & 3 deletions std/digest/crc.d
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ module std.digest.crc;

public import std.digest;

version(unittest) import std.exception;


///
@safe unittest
{
Expand Down Expand Up @@ -661,6 +658,7 @@ alias CRC64ISODigest = WrapperDigest!CRC64ISO;
{
import std.conv : hexString;
import std.range;
import std.exception;

auto crc = new CRC32Digest();

Expand Down
29 changes: 14 additions & 15 deletions std/digest/hmac.d
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,6 @@ if (isDigest!H)
assert(digest == expected);
}

version(unittest)
{
import std.digest : toHexString, LetterCase;
alias hex = toHexString!(LetterCase.lower);
}

@safe pure nothrow @nogc
unittest
{
Expand All @@ -326,10 +320,15 @@ unittest
import std.digest.md : MD5;
import std.digest.sha : SHA1, SHA256;

// Note, can't be UFCS because we don't want to import inside
// version(unittest).
import std.digest : toHexString, LetterCase;
alias hex = toHexString!(LetterCase.lower);

ubyte[] nada;
assert(hmac!MD5 (nada, nada).hex == "74e6f7298a9c2d168935f58c001bad88");
assert(hmac!SHA1 (nada, nada).hex == "fbdb1d1b18aa6c08324b7d64b71fb76370690e1d");
assert(hmac!SHA256(nada, nada).hex == "b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad");
assert(hex(hmac!MD5 (nada, nada)) == "74e6f7298a9c2d168935f58c001bad88");
assert(hex(hmac!SHA1 (nada, nada)) == "fbdb1d1b18aa6c08324b7d64b71fb76370690e1d");
assert(hex(hmac!SHA256(nada, nada)) == "b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad");

import std.string : representation;
auto key = "key".representation,
Expand All @@ -339,13 +338,13 @@ unittest
data2 = "jumps over the lazy dog".representation,
data = data1 ~ data2;

assert(data.hmac!MD5 (key).hex == "80070713463e7749b90c2dc24911e275");
assert(data.hmac!SHA1 (key).hex == "de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9");
assert(data.hmac!SHA256(key).hex == "f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8");
assert(hex(data.hmac!MD5 (key)) == "80070713463e7749b90c2dc24911e275");
assert(hex(data.hmac!SHA1 (key)) == "de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9");
assert(hex(data.hmac!SHA256(key)) == "f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8");

assert(data.hmac!MD5 (long_key).hex == "e1728d68e05beae186ea768561963778");
assert(data.hmac!SHA1 (long_key).hex == "560d3cd77316e57ab4bba0c186966200d2b37ba3");
assert(data.hmac!SHA256(long_key).hex == "a1b0065a5d1edd93152c677e1bc1b1e3bc70d3a76619842e7f733f02b8135c04");
assert(hex(data.hmac!MD5 (long_key)) == "e1728d68e05beae186ea768561963778");
assert(hex(data.hmac!SHA1 (long_key)) == "560d3cd77316e57ab4bba0c186966200d2b37ba3");
assert(hex(data.hmac!SHA256(long_key)) == "a1b0065a5d1edd93152c677e1bc1b1e3bc70d3a76619842e7f733f02b8135c04");

assert(hmac!MD5 (key).put(data1).put(data2).finish == data.hmac!MD5 (key));
assert(hmac!SHA1 (key).put(data1).put(data2).finish == data.hmac!SHA1 (key));
Expand Down
8 changes: 1 addition & 7 deletions std/digest/sha.d
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,6 @@ else version(D_InlineAsm_X86_64)

version(LittleEndian) import core.bitop : bswap;


version(unittest)
{
import std.exception;
}


public import std.digest;

/*
Expand Down Expand Up @@ -1252,6 +1245,7 @@ alias SHA512_256Digest = WrapperDigest!SHA512_256; ///ditto
@system unittest
{
import std.conv : hexString;
import std.exception;
auto sha = new SHA1Digest();

sha.put(cast(ubyte[])"abcdef");
Expand Down
154 changes: 49 additions & 105 deletions std/encoding.d
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,55 @@ import std.typecons;
"\uFFFD","\uFFFD","\uFFFD","\uFFFD","\uFFFD","\uFFFD","\uFFFD","\uFFFD",
];

// HELPER FUNCTIONS
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, these were copied as-is to this unittest. I'm not in love with the implementation, but wanted to keep the PR simple.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This movaroo wasn't quite needed because it doesn't import anything. But fine by me.

// we can probably do this better...
static char toHexDigit(int n)
{
return "0123456789ABCDEF"[n & 0xF];
}

static string makeReadable(string s)
{
string r = "\"";
foreach (char c;s)
{
if (c >= 0x20 && c < 0x80)
{
r ~= c;
}
else
{
r ~= "\\x";
r ~= toHexDigit(c >> 4);
r ~= toHexDigit(c);
}
}
r ~= "\"";
return r;
}

void transcodeReverse(Src,Dst)(immutable(Src)[] s, out immutable(Dst)[] r)
{
static if (is(Src == Dst))
{
return s;
}
else static if (is(Src == AsciiChar))
{
transcodeReverse!(char,Dst)(cast(string) s,r);
}
else
{
foreach_reverse (d;codePoints(s))
{
foreach_reverse (c;codeUnits!(Dst)(d))
{
r = c ~ r;
}
}
}
}

// Make sure everything that should be valid, is
foreach (a;validStrings)
{
Expand Down Expand Up @@ -3633,111 +3682,6 @@ class EncodingSchemeUtf32Native : EncodingScheme
//=============================================================================


// Helper functions
version(unittest)
{
void transcodeReverse(Src,Dst)(immutable(Src)[] s, out immutable(Dst)[] r)
{
static if (is(Src == Dst))
{
return s;
}
else static if (is(Src == AsciiChar))
{
transcodeReverse!(char,Dst)(cast(string) s,r);
}
else
{
foreach_reverse (d;codePoints(s))
{
foreach_reverse (c;codeUnits!(Dst)(d))
{
r = c ~ r;
}
}
}
}

string makeReadable(string s)
{
string r = "\"";
foreach (char c;s)
{
if (c >= 0x20 && c < 0x80)
{
r ~= c;
}
else
{
r ~= "\\x";
r ~= toHexDigit(c >> 4);
r ~= toHexDigit(c);
}
}
r ~= "\"";
return r;
}

string makeReadable(wstring s)
{
string r = "\"";
foreach (wchar c;s)
{
if (c >= 0x20 && c < 0x80)
{
r ~= cast(char) c;
}
else
{
r ~= "\\u";
r ~= toHexDigit(c >> 12);
r ~= toHexDigit(c >> 8);
r ~= toHexDigit(c >> 4);
r ~= toHexDigit(c);
}
}
r ~= "\"w";
return r;
}

string makeReadable(dstring s)
{
string r = "\"";
foreach (dchar c; s)
{
if (c >= 0x20 && c < 0x80)
{
r ~= cast(char) c;
}
else if (c < 0x10000)
{
r ~= "\\u";
r ~= toHexDigit(c >> 12);
r ~= toHexDigit(c >> 8);
r ~= toHexDigit(c >> 4);
r ~= toHexDigit(c);
}
else
{
r ~= "\\U00";
r ~= toHexDigit(c >> 20);
r ~= toHexDigit(c >> 16);
r ~= toHexDigit(c >> 12);
r ~= toHexDigit(c >> 8);
r ~= toHexDigit(c >> 4);
r ~= toHexDigit(c);
}
}
r ~= "\"d";
return r;
}

char toHexDigit(int n)
{
return "0123456789ABCDEF"[n & 0xF];
}
}

/** Definitions of common Byte Order Marks.
The elements of the `enum` can used as indices into `bomTable` to get
matching `BOMSeq`.
Expand Down