Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6982657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:19:04+00:00 2026-05-27T18:19:04+00:00

For learning purposes, I dived into the field of unit testing. I’ve read through

  • 0

For learning purposes, I dived into the field of unit testing. I’ve read through a few tutorials concerning this issue using QT, and came up with the following:

class QMyUnitTest : public QObject
{
    Q_OBJECT
private:
    bool isPrime(unsigned int ui);
private Q_SLOTS:
    void myTest();
};

bool QMyUnitTest::isPrime(unsigned int n) {
    typedef std::map<unsigned int, bool> Filter;
    Filter filter;

    for(unsigned int ui = 2; ui <= n; ui++) {
        filter[ui] = true;
    }
    unsigned int ui = filter.begin()->first;
    for(Filter::iterator it = filter.begin();
        it != filter.end(); it++) {
        if(it->second) {
            for(unsigned int uj = ui * ui; uj <= n; uj += ui) {
                filter[uj] = false;
            }
        }
        ui++;
    }
    return filter[n];
}

void QMyUnitTest::myTest() {
}

QTEST_MAIN(QMyUnitTest)
#include "tst_myunittest.moc"

I know my prime finding algorithm is inefficient and especially flawed; it is meant this way. Now I want to test it thoroughly, but the following question arose:

To test properly, do I not have to have a very precise idea of what could go wrong?

Of course I can run through the first 1000 prime numbers and check if they come out true or 1000 not prime numbers and check if they come out false, but that might not catch the flaws in the algorithm (for example: return filter[n]; is obviously horrible as filter[n] might not exist if n<2).

What use is unit testing if I already have to know what potential problems of my function are?

Am I doing something wrong? Is there a better way to test?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:19:05+00:00Added an answer on May 27, 2026 at 6:19 pm

    The purpose of unit testing is to verify that the code you wrote actually does what it is supposed to do.

    To write correct and complete tests, you need to know precisely what your code is supposed to do. You then write tests to verify:

    • normal conditions (i.e. “normal”, “usual” inputs produce the expected output)
    • border conditions (i.e. input that is just barely within spec)
    • failure conditions (i.e. you verify that your code fails correctly when the input is invalid).

    Your example of testing the behavior of your isPrime routine with a numbers it doesn’t handle is a good one. Without knowing your implementation, 0, 1, 2 and negative values would be good testcases for an isPrime routine – they are things you might not think about when implementing your algorithm, so they are valuable tests.

    Note that verifying normal conditions isn’t necessarily the easiest part. As in this case, making sure your algorithm is perfect requires a mathematical analysis, then a verification that your code implements that correctly – and this is sometimes hard. Checking a few hundred known values is not necessarily enough (it might start failing at the 101st value).

    What use is unit testing if I already have to know what potential problems of my function are?

    You’ve got it reversed. Don’t write your unit tests with your implementation code in mind. Write it with the specification in mind. Your code must fit the spec, and your tests must ensure that as much as they can. (Code coverage tools will help you find border conditions once the bulk of your testing is done.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking through some code for learning purposes. I'm working through this portion of
This is me going right back to basics with TDD for learning purposes. I
This question is for learning purposes. Suppose I am writing a simple SQL admin
For learning purposes I created a toy project using grails. In this project I
I'm writing a script (mostly for learning purposes) for logging into a database, using
This is for learning purposes. I wrote a script that will simulate typing. The
I have been use the rails console for testing and learning purposes and when
So I am just doing a simple email verification for testing/learning purposes, however I
I'm crafting this what you are listening to - plugin for learning purposes that
I'm currently trying to write a website for testing / learning purposes that will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.