Skip to content

Commit

Permalink
Merge pull request #3572 from JackStouffer/issue5945
Browse files Browse the repository at this point in the history
Fix Issue 5945: redBlackTree printing
  • Loading branch information
schveiguy committed Sep 3, 2015
2 parents 13d9dbd + 0f05183 commit 1272c1a
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions std/container/rbtree.d
Expand Up @@ -20,6 +20,7 @@ Authors: Steven Schveighoffer, $(WEB erdani.com, Andrei Alexandrescu)
module std.container.rbtree;

import std.functional : binaryFun;
import std.format;

public import std.container.util;

Expand Down Expand Up @@ -1669,6 +1670,16 @@ assert(equal(rbt[], [5]));
}
}

/**
Formats the RedBlackTree into a sink function. For more info see
$(D std.format.formatValue)
*/
void toString(scope void delegate(const(char)[]) sink, FormatSpec!char fmt) const {
sink("RedBlackTree(");
sink.formatValue(this[], fmt);
sink(")");
}

/**
* Constructor. Pass in an array of elements, or individual elements to
* initialize the tree with.
Expand Down Expand Up @@ -1825,6 +1836,7 @@ pure unittest
pure unittest
{
import std.array : array;

auto rt1 = redBlackTree(5, 4, 3, 2, 1);
assert(rt1.length == 5);
assert(array(rt1[]) == [1, 2, 3, 4, 5]);
Expand All @@ -1842,6 +1854,19 @@ pure unittest
assert(array(rt4[]) == ["hello"]);
}

unittest {
import std.conv : to;

auto rt1 = redBlackTree!string();
assert(rt1.to!string == "RedBlackTree([])");

auto rt2 = redBlackTree!string("hello");
assert(rt2.to!string == "RedBlackTree([\"hello\"])");

auto rt3 = redBlackTree!string("hello", "world", "!");
assert(rt3.to!string == "RedBlackTree([\"!\", \"hello\", \"world\"])");
}

//constness checks
unittest
{
Expand Down

0 comments on commit 1272c1a

Please sign in to comment.