Search code examples
c++unit-testinggoogletest

C++ EXPECT_EQ & ASSERT inside for loop


I'm fairly new to C++ and this is my first time using googletest. I'm trying to implement a code that would simulate a network of inhibitory and excitatory neurons. Every neurone receives inputs from 10% of the population. The spike is received with a delay and stored in a buffer so when the target neuron updates its membrane potential, it can easily read the inputs. Now, I would like to test that when one neuron spikes, its input is written at the right index with the right value. So first I want to loop over every neuron of the population, and then for every neuron I would like to loop over each of its targets and check the buffer of every one; so at the end of my second loop, I have my EXPECT & ASSERT statements. Everything seems to work fine, except I have a feeling it's not testing for all targets and neurons but only the last neuron and its last target (?). Indeed, when I run the test on my terminal it says: 1 test from BufferTest, Run OK. It should be much more tests no ?

Here is my code for that test:

TEST(BufferTest, write_read_resetBuffer) {

Network* net = new Network({0, 20}, 0.1, 1.5, 0.1, 20, 80, 2, 5, "BufferTest.txt");
Neuron* source = new Neuron(0, 0.1,1.5, 0.1, 2, true, 5);
Neuron* Ni = new Neuron(0, 0.1,1.5, 0.1, 2, 5, true);

bool spiking = false; 
for(unsigned int j = 0; j < net->getPop(); ++j) 
{
        source = net->getPopulation_()[j];
        source->set_iext(2);

        do{ 
            spiking = source->update_state_(); 
        }while(!spiking);

        unsigned int index = (source->getspiketime() + 15)%16;      

        for(unsigned int i = 0; i < source->getMyTargets().size(); ++i) 
        {
            int target_i = source->getMyTargets()[i];       

            Ni = net->getPopulation_()[target_i]; 
            Ni->set_step(source->getspiketime());               
            Ni->write_buffer(source->getweight()); 
            std::vector<double> temp = Ni->getBuffer(); 

            ASSERT_EQ(16, temp.size());  
            ASSERT_LE(index, temp.size()); 

            Ni->set_step(15); 
            double value = Ni->read_buffer();               
            Ni->reset_buffer();                     
            if(source->Is_it_excitatory()) 
            { EXPECT_EQ(0.1, value); }else{ EXPECT_EQ(-0.5, value); }; 
            EXPECT_EQ(0, Ni->read_buffer()); 
        }
}
    delete net; 
    net = nullptr; 

}


Solution

  • This:

    TEST(BufferTest, write_read_resetBuffer) {
        ...
    }
    

    is one test, write_read_resetBuffer, from one test case, BufferTest. It doesn't matter how many EXPECT/ASSERT calls you make inside it. Any EXPECT failures will be individually reported in the output of this test. There can be at most one ASSERT failure in a test, of course, because any ASSERT failure ends the test.

    If you want to see one test per EXPECT/ASSERT call then you have to write more TESTs, each containing only one EXPECT/ASSERT. You could put them in the same test case:

    TEST(BufferTest, anotherTest) {
        ...
    }
    

    or in different test cases:

    TEST(AnotherBufferTest, someTest) {
        ...
    }
    

    as you see fit. But there is nothing wrong with what you've done. Your single test has passed.