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

  • Home
  • SEARCH
  • 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 313231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:05:21+00:00 2026-05-12T08:05:21+00:00

In the type of embedded programming I’m getting into, determinism and transparency of the

  • 0

In the type of embedded programming I’m getting into, determinism and transparency of the running code are highly valued. What I mean by transparency is, for instance, being able to look at arbitrary sections of memory and know what variable is stored there. So, as I’m sure embedded programmers expect, new is to be avoided if at all possible, and if it can’t be avoided, then limited to initialization.

I understand the need for this, but don’t agree with the way my coworkers have gone about doing this, nor do I know a better alternative.

What we have are several global arrays of structures, and some global classes. There is one array of structs for mutexes, one for semaphores, and one for message queues (these are initialized in main). For each thread that runs, the class that owns it is a global variable.

The biggest problem I have with this is in unit testing. How can I insert a mock object when the class I want to test #includes global variables that I don’t?

Here’s the situation in pseudo-code:

foo.h

#include "Task.h"
class Foo : Task {
public:
  Foo(int n);
  ~Foo();
  doStuff();
private:
  // copy and assignment operators here
}

bar.h

#include <pthread.h>
#include "Task.h"

enum threadIndex { THREAD1 THREAD2 NUM_THREADS };
struct tThreadConfig {
  char      *name,
  Task      *taskptr,
  pthread_t  threadId,
  ...
};
void startTasks();

bar.cpp

#include "Foo.h"

Foo foo1(42);
Foo foo2(1337);
Task task(7331);

tThreadConfig threadConfig[NUM_THREADS] = {
  { "Foo 1", &foo1, 0, ... },
  { "Foo 2", &foo2, 0, ... },
  { "Task",  &task, 0, ... }
};

void FSW_taskStart() {
    for (int i = 0; i < NUMBER_OF_TASKS; i++) {
        threadConfig[i].taskptr->createThread(  );
    }
}

What if I want more or less tasks? A different set of arguments in the constructor of foo1? I think I would have to have a separate bar.h and bar.cpp, which seems like a lot more work than necessary.

  • 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-12T08:05:21+00:00Added an answer on May 12, 2026 at 8:05 am

    If you want to unit test such code first I would recommend reading Working Effectively With Legacy Code Also see this.

    Basically using the linker to insert mock/fake objects and functions should be a last resort but is still perfectly valid.

    However you can also use inversion of control, without a framework this can push some responsibility to the client code. But it really helps testing. For instance to test FSW_taskStart()

    tThreadConfig threadConfig[NUM_THREADS] = {
      { "Foo 1", %foo1, 0, ... },
      { "Foo 2", %foo2, 0, ... },
      { "Task",  %task, 0, ... }
    };
    
    void FSW_taskStart(tThreadConfig configs[], size_t len) {
        for (int i = 0; i < len; i++) {
            configs[i].taskptr->createThread(  );
        }
    }
    
    void FSW_taskStart() {
        FSW_taskStart(tThreadConfig, NUM_THREADS);
    }
    
    void testFSW_taskStart() {
        MockTask foo1, foo2, foo3;
        tThreadConfig mocks[3] = {
              { "Foo 1", &foo1, 0, ... },
              { "Foo 2", &foo2, 0, ... },
              { "Task",  &foo3, 0, ... }
            };
        FSW_taskStart(mocks, 3);
        assert(foo1.started);
        assert(foo2.started);
        assert(foo3.started);
    }
    

    Now you can can can pass mock version of you’re threads to ‘FSW_taskStart’ to ensure that the function does in fact start the threads as required. Unfortunatly you have to rely on the fact that original FSW_taskStart passes the correct arguments but you are now testing a lot more of your code.

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

Sidebar

Related Questions

How to play a embedded code in lightbox type pop up? Here is the
Displaying Type here to ... until the user enters text into a TextBox is
I have grep function in C( embedded programming ) that takes a void pointer
I've always used typedef in embedded programming to avoid common mistakes: int8_t - 8
On an embedded type system, I have created a Small Object Allocator that piggy
Variables of type int are allegedly one machine-type word in length but in embedded
Can someone guide me how to save an OLE embedded object (Object Type ->
I have an embedded device running a slimmed down version of a HTTP server.
I am working on an embedded system running Linux on a DSP. Now we
I am experimenting with RavenDB embedded in my application. I am receiving a Type

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.