DEV Community

Gcobani Mkontwana
Gcobani Mkontwana

Posted on

How to create a test function and compile it using c++?

Hi Team

I have a class that uses std::map, but i am struggling to create a test function that will test my logic scenario. The scenario is i have implemented a logic to function called void assign that loop through value associate with it and does not duplicate it.

How to i then create a test function from my class to test this scenario using c++?

`class interval_map {
friend void IntervalMapTest();
V m_valBegin;
typedef std::map m_map;

public:

interval_map(V const& val)
: m_valBegin(val)
{}
Enter fullscreen mode Exit fullscreen mode

void assign( K const& keyBegin, K const& keyEnd, V const& val )
{
for( auto it = m_map.find( keyBegin ); * it != keyEnd; ++it )
{
* it = val;
}
}

// look-up of the value associated with key
V const& operator[]( K const& key ) const {
    auto it=m_map.upper_bound(key);
    if(it==m_map.begin()) {
        return m_valBegin;
    } else {
        return (--it)->second;
    }
}
Enter fullscreen mode Exit fullscreen mode

};

int main()
{
m_map map;
m_map::iterator mpIter;

int keyEnd, keyBegin;
int count;

// keep track of values with a std::set
std::set<std::string> values;

for(count = 0; count < 3; count++)
{
    cin >> key;
    cin >> value;

    auto found = map.find(key);

    if(found != map.end()) // key already in map
        continue; // don't add it again


}


return 0;
Enter fullscreen mode Exit fullscreen mode

}`


Enter fullscreen mode Exit fullscreen mode

Top comments (0)