Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[non to!string] because they are unrelated to formatValue
  • Loading branch information
9rnsr committed Jun 20, 2013
1 parent 84a0a41 commit 01f6a97
Showing 1 changed file with 58 additions and 44 deletions.
102 changes: 58 additions & 44 deletions std/conv.d
Expand Up @@ -290,7 +290,7 @@ template to(T)
}

// Tests for issue 6175
unittest
@safe pure unittest
{
char[9] sarr = "blablabla";
auto darr = to!(char[])(sarr);
Expand All @@ -299,14 +299,14 @@ unittest
}

// Tests for issue 7348
unittest
@safe pure unittest
{
assert(to!string(null) == "null");
assert(text(null) == "null");
}

// Tests for issue 8729: do NOT skip leading WS
unittest
@safe pure unittest
{
foreach (T; TypeTuple!(byte, ubyte, short, ushort, int, uint, long, ulong))
{
Expand Down Expand Up @@ -359,14 +359,14 @@ T toImpl(T, S)(S value)
return value;
}

unittest
@safe pure unittest
{
enum E { a } // Issue 9523 - Allow identity enum conversion
auto e = to!E(E.a);
assert(e == E.a);
}

unittest
@safe pure unittest
{
debug(conv) scope(success) writeln("unittest @", __FILE__, ":", __LINE__, " succeeded.");
int a = 42;
Expand All @@ -375,7 +375,7 @@ unittest
}

// Tests for issue 6377
unittest
@safe pure unittest
{
// Conversion between same size
foreach (S; TypeTuple!(byte, short, int, long))
Expand Down Expand Up @@ -452,7 +452,7 @@ T toImpl(T, S)(ref S s)
return toImpl!(T, typeof(s[0])[])(s);
}

unittest
@safe pure unittest
{
char[4] test = ['a', 'b', 'c', 'd'];
static assert(!isInputRange!(Unqual!(char[4])));
Expand All @@ -469,7 +469,7 @@ T toImpl(T, S)(S value)
return value.opCast!T();
}

unittest
@safe pure unittest
{
debug(conv) scope(success) writeln("unittest @", __FILE__, ":", __LINE__, " succeeded.");
class B
Expand Down Expand Up @@ -501,7 +501,7 @@ T toImpl(T, S)(S value)
}

// Bugzilla 3961
unittest
@safe pure unittest
{
debug(conv) scope(success) writeln("unittest @", __FILE__, ":", __LINE__, " succeeded.");
struct Int
Expand All @@ -513,14 +513,14 @@ unittest
static struct Int2
{
int x;
this(int x) { this.x = x; }
this(int x) @safe pure { this.x = x; }
}
Int2 i2 = to!Int2(1);

static struct Int3
{
int x;
static Int3 opCall(int x)
static Int3 opCall(int x) @safe pure
{
Int3 i;
i.x = x;
Expand All @@ -531,11 +531,11 @@ unittest
}

// Bugzilla 6808
unittest
@safe pure unittest
{
static struct FakeBigInt
{
this(string s){}
this(string s) @safe pure {}
}

string s = "101";
Expand All @@ -550,7 +550,7 @@ T toImpl(T, S)(S value)
return new T(value);
}

unittest
@safe pure unittest
{
static struct S
{
Expand All @@ -559,14 +559,14 @@ unittest
static class C
{
int x;
this(int x) { this.x = x; }
this(int x) @safe pure { this.x = x; }
}

static class B
{
int value;
this(S src) { value = src.x; }
this(C src) { value = src.x; }
this(S src) @safe pure { value = src.x; }
this(C src) @safe pure { value = src.x; }
}

S s = S(1);
Expand All @@ -581,17 +581,17 @@ unittest
assert(c2.x == 3);
}

unittest
@safe pure unittest
{
struct S
{
class A
{
this(B b) {}
this(B b) @safe pure {}
}
class B : A
{
this() { super(this); }
this() @safe pure { super(this); }
}
}

Expand All @@ -602,14 +602,14 @@ unittest

static class C : Object
{
this() {}
this(Object o) {}
this() @safe pure {}
this(Object o) @safe pure {}
}

Object oc = new C();
C a2 = to!C(oc); // == new C(a)
// Construction conversion overrides down-casting conversion
assert(a2 != a); //
assert(a2 !is a); //
}

/**
Expand Down Expand Up @@ -657,7 +657,7 @@ T toImpl(T, S)(S value)
}
static assert(isModConvertible, "Bad modifier conversion: "~S.stringof~" to "~T.stringof);

auto result = cast(T) value;
auto result = ()@trusted{ return cast(T) value; }();
if (!result && value)
{
throw new ConvException("Cannot convert object of static type "
Expand All @@ -667,7 +667,7 @@ T toImpl(T, S)(S value)
return result;
}

unittest
@safe pure unittest
{
debug(conv) scope(success) writeln("unittest @", __FILE__, ":", __LINE__, " succeeded.");
// Testing object conversions
Expand All @@ -681,7 +681,7 @@ unittest
}

// Unittest for 6288
unittest
@safe pure unittest
{
template Identity(T) { alias T Identity; }
template toConst(T) { alias const(T) toConst; }
Expand Down Expand Up @@ -1103,7 +1103,7 @@ body
}
}

unittest
@safe pure unittest
{
foreach (Int; TypeTuple!(uint, ulong))
{
Expand Down Expand Up @@ -1281,10 +1281,10 @@ T toImpl(T, S)(S value)
if (value > T.max)
throw new ConvOverflowException("Conversion positive overflow");
}
return cast(T) value;
return (ref value)@trusted{ return cast(T) value; }(value);
}

unittest
@safe pure unittest
{
dchar a = ' ';
assert(to!char(a) == ' ');
Expand Down Expand Up @@ -1316,19 +1316,18 @@ T toImpl(T, S)(S value)
!isSomeString!S && isDynamicArray!S &&
!isExactSomeString!T && isArray!T)
{
alias typeof(T.init[0]) E;
auto result = new E[value.length];
foreach (i, e; value)
alias E = typeof(T.init[0]);

auto w = appender!(E[])();
w.reserve(value.length);
foreach (i, ref e; value)
{
/* Temporarily cast to mutable type, so we can get it initialized,
* this is ok because there are no other references to result[]
*/
cast()(result[i]) = to!E(e);
w.put(to!E(e));
}
return result;
return w.data;
}

unittest
@safe pure unittest
{
// array to array conversions
debug(conv) scope(success) writeln("unittest @", __FILE__, ":", __LINE__, " succeeded.");
Expand All @@ -1337,8 +1336,8 @@ unittest
auto b = to!(float[])(a);
assert(b == [ 1.0f, 2, 3 ]);

auto c = to!(string[])(b);
assert(c[0] == "1" && c[1] == "2" && c[2] == "3");
//auto c = to!(string[])(b);
//assert(c[0] == "1" && c[1] == "2" && c[2] == "3");

immutable(int)[3] d = [ 1, 2, 3 ];
b = to!(float[])(d);
Expand All @@ -1356,6 +1355,13 @@ unittest
}
Wrap[] warr = to!(Wrap[])(["foo", "bar"]); // should work
}
/*@safe pure */unittest
{
auto b = [ 1.0f, 2, 3 ];

auto c = to!(string[])(b);
assert(c[0] == "1" && c[1] == "2" && c[2] == "3");
}

/**
Associative array to associative array conversion converts each key
Expand All @@ -1365,6 +1371,8 @@ T toImpl(T, S)(S value)
if (isAssociativeArray!S &&
isAssociativeArray!T && !is(T == enum))
{
/* This code is potentially unsafe.
*/
alias KeyType!T K2;
alias ValueType!T V2;

Expand All @@ -1380,7 +1388,7 @@ T toImpl(T, S)(S value)
return cast(T)result;
}

unittest
@safe /*pure */unittest
{
// hash to hash conversions
int[string] a;
Expand All @@ -1389,7 +1397,7 @@ unittest
auto b = to!(double[dstring])(a);
assert(b["0"d] == 1 && b["1"d] == 2);
}
unittest // Bugzilla 8705, from doc
@safe /*pure */unittest // Bugzilla 8705, from doc
{
int[string][double[int[]]] a;
auto b = to!(short[wstring][string[double[]]])(a);
Expand Down Expand Up @@ -1475,7 +1483,7 @@ private void testFloatingToIntegral(Floating, Integral)()
}
}

unittest
@safe pure unittest
{
debug(conv) scope(success) writeln("unittest @", __FILE__, ":", __LINE__, " succeeded.");

Expand Down Expand Up @@ -1552,6 +1560,12 @@ unittest
assert(a == 42);
}
}
}
/*@safe pure */unittest
{
alias AllInts = TypeTuple!(byte, ubyte, short, ushort, int, uint, long, ulong);
alias AllFloats = TypeTuple!(float, double, real);
alias AllNumerics = TypeTuple!(AllInts, AllFloats);
// test conversions to string
{
foreach (T; AllNumerics)
Expand Down Expand Up @@ -1658,7 +1672,7 @@ T toImpl(T, S)(S value)
throw new ConvException(format("Value (%s) does not match any member value of enum '%s'", value, T.stringof));
}

unittest
@safe pure unittest
{
enum En8143 : int { A = 10, B = 20, C = 30, D = 20 }
enum En8143[][] m3 = to!(En8143[][])([[10, 30], [30, 10]]);
Expand Down

0 comments on commit 01f6a97

Please sign in to comment.