Feasible Idea?: Range Tester

Nick Sabalausky SeeWebsiteToContactMe at semitwist.com
Thu Mar 21 13:47:19 PDT 2013


On Thu, 21 Mar 2013 21:41:52 +0100
"jerro" <a at a.com> wrote:

> > void testForwardRange(R, E)(R range, E[] content)
> > {
> >     // Deliberately not contraints, because this *is* a test, 
> > after all.
> >     // *Or* maybe it just auto-detects range type and leaves it
> >     // up to the user to check for "isForwardRange!R" or 
> > whatever.
> >     static assert(isForwardRange!R);
> >     static assert(is(ElementTypeOf!R == E));
> >
> >     static if(hasLength!R)
> >         assert(range.length == content.length);
> > 
> >     foreach(i; 0...content.length)
> >     {
> >         assert(range.front == content[i]);
> >         range.popFront();
> >     }
> >
> >     // More tests
> > }
> 
> Would it make sense to just name the function testRange and have 
> it test conformance for any range primitives that the type 
> supports? Your example already uses static "if(hasLenght!R)" for 
> length, it may be a good idea to expand that.

Possibly. After all, a person really should already be using "static
assert(isBlahBlahRange!MyRange);" to make sure their range is the
type they expect it to be anyway.



More information about the Digitalmars-d mailing list