www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Problem with unittest in templates.

reply "Peter C. Chapin" <pchapin sover.net> writes:
Hello! I'm using dmd 0.178. I'm having trouble getting the unittest 
section of a class template to execute. I have two files:

----> main.d <----

import other;

int main( )
{
  Foo!(int) my_foo = new Foo!(int);
  return( 0 );
}

----> other.d <----

class Foo(T) {
  unittest {
    assert( 1 == 0 );
  }
};

I compile this program using 'dmd -unittest main.d other.d'. It compiles 
without error but when it executes there is no assertion failure. 
However, if I move the definition of class Foo(T) into main.d (and throw 
away other.d) I *do* get the assertion failure. Am I doing something 
wrong? Is this supposed to work?

I'm also noticing that the unittest section isn't as useful in a 
template as it is in a non-template. It's awkward writing unittests 
generically without knowledge of a specific type T. Is this "the way it 
is" or is there some nice programming technique that I should be using 
here? I find myself thinking about writing a separate test program (C++ 
style) where I can work with specific specializations of the template.

Peter
Dec 29 2006
next sibling parent reply Thomas Kuehne <thomas-dloop kuehne.cn> writes:
Peter C. Chapin <pchapin sover.net> schrieb:
 Hello! I'm using dmd 0.178. I'm having trouble getting the unittest 
 section of a class template to execute. I have two files:

 ----> main.d <----

 import other;

 int main( )
 {
   Foo!(int) my_foo = new Foo!(int);
   return( 0 );
 }

 ----> other.d <----

 class Foo(T) {
   unittest {
     assert( 1 == 0 );
   }
 };

 I compile this program using 'dmd -unittest main.d other.d'. It compiles 
 without error but when it executes there is no assertion failure. 
 However, if I move the definition of class Foo(T) into main.d (and throw 
 away other.d) I *do* get the assertion failure. Am I doing something 
 wrong? Is this supposed to work?
Please file a bug report: http://d.puremagic.com/issues Thomas
Dec 29 2006
parent "Peter C. Chapin" <pchapin sover.net> writes:
Thomas Kuehne <thomas-dloop kuehne.cn> wrote in
news:slrnepaeuk.8ki.gast birke.kuehne.cn: 

 Please file a bug report:
 http://d.puremagic.com/issues
Done. Peter
Dec 30 2006
prev sibling next sibling parent reply Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
Peter C. Chapin wrote:
 Hello! I'm using dmd 0.178. I'm having trouble getting the unittest 
 section of a class template to execute. I have two files:
 
 ----> main.d <----
 
 import other;
 
 int main( )
 {
   Foo!(int) my_foo = new Foo!(int);
   return( 0 );
 }
 
 ----> other.d <----
 
 class Foo(T) {
   unittest {
     assert( 1 == 0 );
   }
 };
 
 I compile this program using 'dmd -unittest main.d other.d'. It compiles 
 without error but when it executes there is no assertion failure. 
 However, if I move the definition of class Foo(T) into main.d (and throw 
 away other.d) I *do* get the assertion failure. Am I doing something 
 wrong? Is this supposed to work?
 
 I'm also noticing that the unittest section isn't as useful in a 
 template as it is in a non-template. It's awkward writing unittests 
 generically without knowledge of a specific type T. Is this "the way it 
 is" or is there some nice programming technique that I should be using 
 here? I find myself thinking about writing a separate test program (C++ 
 style) where I can work with specific specializations of the template.
 
 Peter
While this is most certainly a bug, for the meantime you could try writing a module level unittest block for your template. Something like: You won't be writing a "generic" test, but you should be able to check the particular things you're wanting to. -- Chris Nicholson-Sauls
Dec 29 2006
parent "Peter C. Chapin" <pchapin sover.net> writes:
Chris Nicholson-Sauls <ibisbasenji gmail.com> wrote in
news:en3n33$1vck$1 digitaldaemon.com: 

 While this is most certainly a bug, for the meantime you could try
 writing a module level unittest block for your template.  Something
 like: 
 






That's cool. I didn't realize you could do that. This has the advantage in this case of not requiring the unittest block to be generic. I find building completely generic tests that are also reasonably exhaustive to be something of a challenge. Peter
Dec 30 2006
prev sibling parent Jason House <jason.james.house gmail.com> writes:
Peter C. Chapin wrote:
 Hello! I'm using dmd 0.178. I'm having trouble getting the unittest 
 section of a class template to execute. I have two files:
 
 ----> main.d <----
 
 import other;
 
 int main( )
 {
   Foo!(int) my_foo = new Foo!(int);
   return( 0 );
 }
 
 ----> other.d <----
 
 class Foo(T) {
   unittest {
     assert( 1 == 0 );
   }
 };
 
 I compile this program using 'dmd -unittest main.d other.d'. It compiles 
 without error but when it executes there is no assertion failure. 
 However, if I move the definition of class Foo(T) into main.d (and throw 
 away other.d) I *do* get the assertion failure. Am I doing something 
 wrong? Is this supposed to work?
 
 I'm also noticing that the unittest section isn't as useful in a 
 template as it is in a non-template. It's awkward writing unittests 
 generically without knowledge of a specific type T. Is this "the way it 
 is" or is there some nice programming technique that I should be using 
 here? I find myself thinking about writing a separate test program (C++ 
 style) where I can work with specific specializations of the template.
 
 Peter
In my experiments with D, I've found that you must instantiate the templated class in order to get the unittest to run. I don't know if it's one unit test execution per instantiation or one unit test run for each complete type (such as T=int, T=char, etc...). I'd guess the latter.
Dec 30 2006