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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:59:38+00:00 2026-05-13T01:59:38+00:00

I am working on a quite large C library that doesn’t have any tests

  • 0

I am working on a quite large C library that doesn’t have any tests now. As the API starts to be final, I’d like to start writing unit tests.

Nearly all my functions acts on the first parameter (a structure).

The naive approach to unit test is to have the pre function call structure in a known state, call the function, and then compare the pre call structure with the expected result.

Now this works with structure composed of scalar types, but as for allocated memory, I was wondering what kind of approach you were using.

For example, imagine an image structure, when you do:

CreateImage(&img, x, y);

you expect the img->x to be x, img->y to be y and img->pixels to be a pointer to something big enough to hold x * y * sizeof(pixel).

Checking for the first two is trivial, but what about the img->pixels? I don’t mean to check if the malloc call was successful, as I can overload malloc, but I want to know if malloc was called properly.

This is especially important in case like that:

CreateImage(*img, x, y)
{
    img->x = x; img->y = y;
    /* do something, dhoo, that something is broken and modify x or y */
    img->pixels = malloc(x * y * sizeof(pixel)); /* wrong allocation size */
    if(!img->pixels) error("no memory");
}

I hope my question is clear.

Thanks.

  • 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-13T01:59:38+00:00Added an answer on May 13, 2026 at 1:59 am

    In your unit test, have your overloaded malloc function log the value that it was called with (eg. alloc_size).

    Then just test that all of these are true:

    • alloc_size % sizeof(pixel) == 0
    • alloc_size % x == 0
    • alloc_size % y == 0
    • ((alloc_size / sizeof(pixel)) / x ) / y == 1

    Do not just replicate the code from the function you’re testing – ie. the multiplication – because you may end up replicating a bug.

    In this case you would potentially replicate a bug – if there’s no bounds tests on x and y, then the multiplication in the malloc call is itself buggy (consider what would happen on a system with a 32 bit size_t, sizeof(pixel) == 4, x == 40000 and y == 40000).

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

Sidebar

Related Questions

We've been working on an application that quite heavily relies on VirtualPathProviders in ASP.NET.
I am working on a Windows Forms app for quite some time now, and
First, a bit of my background. I have been working on large web systems
I have written a fairly large program in Fortran 90. It has been working
I'm working on a large Winforms project for a client that is using business
I have been working with the Winforms ReportViewer control for a while now. I
I have a MySQL database that contains a large number of polygons, one for
I am using the code snippet below, however it's not working quite as I
I've been quite used to working on small projects which I coded with 1,000
I'm working on my first Ruby on Rails aplication and it's quite big (at

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.