Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-03-2023, 12:05 PM   #1
Senior Member
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,017

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
Simple reminder to new coders and possibly old hands alike. unit tests.

I spent the last 2 days staring at a library function I had written trying to figure out why it was seg-faulting. It worked fine in another program I was hacking at so it was annoying to say the least.


Had I not been dumb I would have made sure the file opened successfully. I had manually created the file and put the wrong file name in my global #define for that string. As such it failed to open every single time. Since I didn't have a prompt or error break on failure to open it would just segfault when my function to count the number of lines ran. I wasted a couple days getting pissed off for no reason.

The C course I started my journey with was on Udemy. The instructor wouldn't stop talking about unit tests at one point. Probably the most important lesson I've learned thus far now. Especially after wasting so much time for no reason.

Validate everything. While there is probably a point where it gets out of hand it will only help you to include a test for pretty much every variable and bit of data you compute in your stuff.

if (!fileptr)
  printf("unable to open file!\n");
This small segment of code in this case, which I actually had created a library function with more data output about the error some time ago would have saved me 2 days of screwing around. And likely my blood pressure a bit as well.

Last edited by jmgibson1981; 04-03-2023 at 12:12 PM.
Old 04-03-2023, 01:22 PM   #2
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,479
Blog Entries: 1

Rep: Reputation: 1695Reputation: 1695Reputation: 1695Reputation: 1695Reputation: 1695Reputation: 1695Reputation: 1695Reputation: 1695Reputation: 1695Reputation: 1695Reputation: 1695
Or even:
FILE *efopen (const char *name, const char *mode)
    FILE *f;
    int ern;

    f = fopen (name, mode);

    if (f) return f;
    ern= errno;
    fprintf (stderr, "Error opening file \"%s\" mode \"%s\""
             " errno=%d: %s\n"
            , name, mode, ern, strerror (ern));
    exit (3004);
    return NULL;
Old 04-07-2023, 07:51 PM   #3
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,243
Blog Entries: 4

Rep: Reputation: 3776Reputation: 3776Reputation: 3776Reputation: 3776Reputation: 3776Reputation: 3776Reputation: 3776Reputation: 3776Reputation: 3776Reputation: 3776Reputation: 3776
Over many years, I single-handedly built a software product (in Delphi) which, I am very pleased to say, encountered almost no "runtime errors that made it out into the field." And one of the primary reasons why it did so was because I stuffed the programming which what might now be called assert()s ... and then never took them out. The programming was always built to be "suspicious," and if it detected anything wrong it would throw/raise a clearly-identified "exception." All of these tests were present in the deployed code.

Every now and then, one of these exceptions alerted me to what otherwise would have been "a nasty and hard-to-find bug." Except that it alerted me more-or-less exactly to where and what it was. It has been very many years since an actual customer has ever reported seeing any of these exceptions being thrown, and I never expect to encounter another one.

Likewise: the fact that these tests aren't going off reassures me that the underlying problems aren't happening.

But, I continue to write code in this way: "suspiciously." Because the code that is executing at this moment is in the very best position to recognize that "something has gone wrong." And, especially today, the "overhead" of performing these tests is zero.

Last edited by sundialsvcs; 04-07-2023 at 07:54 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Speeding up open-source GPU driver development with unit tests, drm-shim, and code reuse LXer Syndicated Linux News 0 01-30-2022 04:06 AM
LXer: Perform unit tests using GoogleTest and CTest LXer Syndicated Linux News 0 01-18-2022 10:33 PM
How do we get my UNIT(service) name in unit.sevice Boobalan M Linux - Software 6 01-23-2018 05:03 AM
LXer: Hands-on and under the hood: Ars tests Firefox on Android LXer Syndicated Linux News 0 02-22-2010 07:31 PM
Storage unit and IO unit salmanucit Linux - General 2 06-17-2008 11:11 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:36 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration