DEV Community

Gcobani Mkontwana
Gcobani Mkontwana

Posted on

1 1

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

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs