Thread overview | ||||||
---|---|---|---|---|---|---|
|
May 16, 2005 SegFault from Unit-Test | ||||
---|---|---|---|---|
| ||||
I'm trying to write a class with a built-in unit test, but I get a segment fault every time I try to run it. I've reproduced it with the sample code in on-line the documentation. anchorage:~/tmp> cat simple.d class Sum { int add(int x, int y) { return x + y; } unittest { Sum s; assert(s.add(3,4) == 7); assert(s.add(-2,0) == -2); } } int main(char[][] argv) { /* no problemo */ return 0; } anchorage:~/tmp> dmd -unittest simple.d gcc simple.o -o simple -lphobos -lpthread -lm anchorage:~/tmp> ./simple Segmentation fault NOTE: I had to add the "Sum s;" and "s.add" that do not appear in the on-line version because otherwise it won't compile (complains about need "'this' to access member add"; Walter may want to fix that). However, if instead I change the declaration of "add" to be "static" so that no local instance need be accessed, then it compiles and runs just fine. Any ideas? Is there some restriction about creating an instance of the class under test? Brian ( bcwhite@precidia.com ) ------------------------------------------------------------------------------- There's no healthy way to mess with the line between wrong and right. |
May 16, 2005 Re: SegFault from Unit-Test | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brian White | On Mon, 16 May 2005 10:21:02 -0400, Brian White <bcwhite@precidia.com> wrote:
> I'm trying to write a class with a built-in unit test, but I get a segment fault every time I try to run it. I've reproduced it with the sample code in on-line the documentation.
>
> anchorage:~/tmp> cat simple.d
> class Sum
> {
> int add(int x, int y) { return x + y; }
>
> unittest
> {
> Sum s;
> assert(s.add(3,4) == 7);
> assert(s.add(-2,0) == -2);
> }
> }
>
>
> int main(char[][] argv)
> {
> /* no problemo */
> return 0;
> }
>
> anchorage:~/tmp> dmd -unittest simple.d
> gcc simple.o -o simple -lphobos -lpthread -lm
>
> anchorage:~/tmp> ./simple
> Segmentation fault
You're working with a null reference there. Allocate an instance:
unittest
{
Sum s = new Sum;
assert(s.add(3,4) == 7);
assert(s.add(-2,0) == -2);
}
|
May 16, 2005 Re: SegFault from Unit-Test | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brian White | In article <d6aa8e$1t03$1@digitaldaemon.com>, Brian White says... > >NOTE: I had to add the "Sum s;" and "s.add" that do not appear in the on-line version because otherwise it won't compile (complains about need "'this' to access member add"; Walter may want to fix that). Yes, it's a bug in the documentation (not the compiler.) The unittests have to make an object before they can use non-static members, even if they are declared inside the class. Replace "Sum s;" with "Sum s = new Sum;" and it should work. Hope this helps :) Nick |
May 16, 2005 Re: SegFault from Unit-Test | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vathix | > You're working with a null reference there. Allocate an instance: Ah yes... All classes are references and must be allocated. Didn't even think about that. Thanks! Brian ( bcwhite@precidia.com ) ------------------------------------------------------------------------------- The future is not set. There is no fate but what we make for ourselves. -- JC |
Copyright © 1999-2021 by the D Language Foundation