CppUnit: Why does a static local variable keep its value?

804b18f832fb419fb142

I'm trying to use CppUnit to test a method that should execute some code only the first time it is called.

class CElementParseInputTests: public CppUnit::TestFixture {
private:
    CElement* element;
public:

    void setUp() {
        element = new CElement();
    }

    void tearDown() {
        delete element;
    }

    void test1() {
        unsigned int parsePosition = 0;
        CPPUNIT_ASSERT_EQUAL(false, element->parseInput("fäil", parsePosition));
    }

    void test2() {
        unsigned int parsePosition = 0;
        CPPUNIT_ASSERT_EQUAL(false, element->parseInput("pass", parsePosition));
    }

The recursive method I want to test:

bool CElement::parseInput(const std::string& input, unsigned int& parsePosition) {
    static bool checkedForNonASCII = false;
    if(!checkedForNonASCII) {
        std::cout << "this should be printed once for every test case" << std::endl;
        [...]
        checkedForNonASCII = true;
    }
    [...]
    parseInput(input, parsePosition+1)
    [...]
}

Since the object is recreated and then destroyed for every test case, I would expect that the string "this should be printed once for every test case" would be printed twice when running the tests, but it's only printed once. What have i missed?

songyuanyao

That's what static local variables supposed to do.

Variables declared at block scope with the specifier static have static storage duration but are initialized the first time control passes through their declaration (unless their initialization is zero- or constant-initialization, which can be performed before the block is first entered). On all further calls, the declaration is skipped.

That means checkedForNonASCII will be initialized to false only once for the 1st call. For further calls the initialization is skipped; i.e. checkedForNonASCII won't be initialized to false again.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does the static modifier prevent its variable to be reassigned with a new value?

From Dev

cppunit export local variable

From Dev

Python: why does this local list variable in class method retain its value?

From Dev

Using a local variable outside of its declaring scope; why does this work?

From Dev

Why does this variable need to be static?

From Dev

Why are static local variable in C reloaded?

From Java

Does static make a difference for a const local variable?

From Dev

When does static local variable comes into existence?

From Dev

Store dynamic int value in local variable to static?

From Dev

Why static/member variable are slower than local variable?

From Dev

Why does Flex/Lex reset variable of c++ code to its initial value?

From Dev

Why IGNORECASE awk variable does not work properly when its value is set in the command line option?

From Dev

Why does a C# linked list class variable not retain its value after it's assigned?

From Dev

Java : Why does placement of static variable matter?

From Dev

Why does Leiningen keep its own JVM running?

From Dev

Why does my heroku application data keep resetting its data?

From Dev

Why does the value of this variable change?

From Dev

why does variable not update value

From Dev

How to store static value from variable that keep changes in Javascript

From Dev

Why does a string lose its value?

From Dev

Verilog: Why the "maxcount" cannot keep its max value but changes with the "count"?

From Dev

Verilog: Why the "maxcount" cannot keep its max value but changes with the "count"?

From Dev

Why does global variable stay undefined even after assigning value to it in a local scope in javascript

From Dev

Why is the static variable not assigned to the value returned by the static method?

From Dev

Does a function local static variable automatically incur a branch?

From Dev

ObjC: Local variable seems to retain its value across function calls

From Dev

Initialize a static local variable to a value unknown during compilation

From Dev

Is there a performance advantage to declaring a static value globally over a local variable in Java?

From Dev

Initialize a static local variable to a value unknown during compilation

Related Related

  1. 1

    Why does the static modifier prevent its variable to be reassigned with a new value?

  2. 2

    cppunit export local variable

  3. 3

    Python: why does this local list variable in class method retain its value?

  4. 4

    Using a local variable outside of its declaring scope; why does this work?

  5. 5

    Why does this variable need to be static?

  6. 6

    Why are static local variable in C reloaded?

  7. 7

    Does static make a difference for a const local variable?

  8. 8

    When does static local variable comes into existence?

  9. 9

    Store dynamic int value in local variable to static?

  10. 10

    Why static/member variable are slower than local variable?

  11. 11

    Why does Flex/Lex reset variable of c++ code to its initial value?

  12. 12

    Why IGNORECASE awk variable does not work properly when its value is set in the command line option?

  13. 13

    Why does a C# linked list class variable not retain its value after it's assigned?

  14. 14

    Java : Why does placement of static variable matter?

  15. 15

    Why does Leiningen keep its own JVM running?

  16. 16

    Why does my heroku application data keep resetting its data?

  17. 17

    Why does the value of this variable change?

  18. 18

    why does variable not update value

  19. 19

    How to store static value from variable that keep changes in Javascript

  20. 20

    Why does a string lose its value?

  21. 21

    Verilog: Why the "maxcount" cannot keep its max value but changes with the "count"?

  22. 22

    Verilog: Why the "maxcount" cannot keep its max value but changes with the "count"?

  23. 23

    Why does global variable stay undefined even after assigning value to it in a local scope in javascript

  24. 24

    Why is the static variable not assigned to the value returned by the static method?

  25. 25

    Does a function local static variable automatically incur a branch?

  26. 26

    ObjC: Local variable seems to retain its value across function calls

  27. 27

    Initialize a static local variable to a value unknown during compilation

  28. 28

    Is there a performance advantage to declaring a static value globally over a local variable in Java?

  29. 29

    Initialize a static local variable to a value unknown during compilation

HotTag

Archive