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 6211495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:20:49+00:00 2026-05-24T06:20:49+00:00

Here is the scenario I’ve got. The function I’m testing has an error condition

  • 0

Here is the scenario I’ve got. The function I’m testing has an error condition that, if hit, calls a graceful exit function to free any global memory, close handles, and exit the program.

Obviously, I’ll want to write a test that tickles this condition to make sure that it is handled correctly but I don’t want the graceful exit routine to actually exit the program since that would stop any remaining tests. This means stubbing the graceful exit routine. The problem with stubbing and not calling exit is that the flow of control returns to the function under test (which is bad since the routine was supposed to exit).

Here is the actual question: How do we return control from the stubbed function to the test instead of to the function under test?

I can do a setjmp / longjmp, but since “gotos” are bad in general, I’d love any other suggestions. (Keep in mind that this is procedural C, not C++, so exceptions aren’t going to work as far as I know)

EDIT
As Soren and others have suggested below, making exit do nothing when testing is a great idea. There are several ways to do this whether it be through a #define statement or a stub for the exit() routine.

HOWEVER, doing so presents the problem that I’m really after a solution for (other than setjmp / longjmp). Take a look at this scenario:

void gracefulExit() {
    // Clean Up
    exit();
}

void routineUnderTest() {
    // Do some calcs

    if (someBadCondition == TRUE)
        gracefulExit()

    // Do some more calcs
}

If exit() does nothing in this scenario, gracefulExit() will return control back to the routine under test, which should not happen. Hence, I need a way to make exit() (or the stubbed version of gracefulExit()) return control to the test instead of the function under test.

setjmp / longjmp (aka goto) is a way to do this, although not really an elegant way. Any ideas on how to solve that?

EDIT #2

As fizzer mentioned, setjmp/longjmp is a valid way to handle this situation. It is the likely way that I will handle it permanently.

I have, however, received another possible solution from a co-worker. Instead of #defining the gracefulExit() routine to a stub routine, do the following:

#define gracefulExit return NULL

The particular function under test handles this just fine since NULL is a valid return value for it. I haven’t tested this in every possible case yet (e.g. a function that has a void return value). As mentioned earlier, I will likely use the setjmp/longjmp way of solving this problem, but if this additional solution sparks an idea for somebody, great!

  • 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-24T06:20:50+00:00Added an answer on May 24, 2026 at 6:20 am

    I use setjmp / longjmp (wrapped in a convenience macro) for this and similar scenarios (e.g. test that an assertion failed).

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

Sidebar

Related Questions

Here's my scenario - I have an SSIS job that depends on another prior
Here is the scenario: I'm writing an app that will watch for any changes
Here is the scenario that I have. I have a cvs repository in one
Here's the scenario-- you've got a JSON object on the browser, and let's say
Here's the scenario: You have a Windows server that users remotely connect to via
Here's my scenario. I've got SharePoint 2010 running on one server, with an SQL
Here is the scenario: I have a table that records the user_id, the module_id,
Here is a Scenario, A series of operations that I will call for painting,
I have got an ASP page that does some back-end processing. It calls a
Ok, I'll do my best to explain the problem here: The scenario is that

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.