samedi 23 février 2019

Ensure that we initialize each variable once and only once

Below is a test from LLVM's exception handling library libcxxabi (which by the way uses LLVM's stack unwind library libunwind):

// libcxxabi\test\test_guard.pass.cpp
...
// Ensure that we initialize each variable once and only once.
namespace test1 {
    static int run_count = 0;
    int increment() {
        ++run_count;
        return 0;
    }
    void helper() {
        static int a = increment();
        ((void)a);
    }
    void test() {
        static int a = increment(); ((void)a);
        assert(run_count == 1);
        static int b = increment(); ((void)b);
        assert(run_count == 2);
        helper();
        assert(run_count == 3);
        helper();
        assert(run_count == 3);
    }
}

...

int main()
{
    test1::test();
}

Maybe I'm missing something obvious but I'm not sure what's the idea behind this test (what does it test and how). Do you have any ideas?

Aucun commentaire:

Enregistrer un commentaire