Advice on removing code redundancy

Gedare Bloom gedare at rtems.org
Wed Aug 19 18:46:23 UTC 2015


Use paramterized CPP (C pre-processor) macros with string
concatenation via ##, something like

#define TEST_BODY(test_name, test_num, test_tag) \
void __run_##test_name##_##test_num##() \
{ \
... \
  rtems_task_start (tc_task_id, ___##test_tag##_##test_num##, 0);
... \
} \
rtems_task Init (rtems_task_argument) \
{ \
  tc_finished = false; \
  ___run_##test_name##_##test_num##(); \
...
} \

Something like that, the syntax may be a bit different but that's the idea.

On Wed, Aug 19, 2015 at 10:26 AM, Saeed Ehteshamifar
<salpha.2004 at gmail.com> wrote:
> Hi,
>
> Please take a look at following snippet:
>
> testcase_executer.cpp:
> 1.  #include "___TC_abs_1.h"
> 2.
> 3.  void ___run_tc_abs_1 ()
> 4.  {
> 5.    rtems_id tc_task_id;
> 6.    rtems_name tc_task_name = rtems_build_name( 'T', 'C', ' ', ' ');
> 7.    rtems_task_create(
> 8.    tc_task_name, TC_TASK_PRIORITY, RTEMS_MINIMUM_STACK_SIZE,
> 9.      RTEMS_DEFAULT_MODES, RTEMS_DEFAULT_ATTRIBUTES, &tc_task_id);
> 10.
> 11.   rtems_task_start (tc_task_id, ___TC_abs_1, 0);
> 12.
> 13.   for (int i=0; i<TC_TIMEOUT_DIVISION_FACTOR; i++) {
> 14.     status = rtems_task_wake_after(RTEMS_MICROSECONDS_TO_TICKS(
> 15.       TC_TIMEOUT/TC_TIMEOUT_DIVISION_FACTOR));
> 16.     if (tc_finished)
> 17.       break;
> 18.   }
> 19. }
> 20.
> 21. rtems_task Init (rtems_task_argument)
> 22. {
> 23.   tc_finished = false;
> 24.   ___run_tc_wait_1 ();
> 25.   if (tc_finished)
> 26.     cout << "___TC_wait_1: " << "PASS" << endl;
> 27.   else
> 28.     cout << "___TC_wait_1: " << "RESTART" << endl;
> 29.   cout << endl;
> 30.
> 31.   cout << "finito!" << endl;
> 32.   exit (0);
> 33. }
>
> It's going to be thousands of test cases, and I want to serially run each
> test case in a task other than Init.
> In the code above, I've marked symbols that are per each test case, with 3
> leading underscores (___).
> This is the basic duplication structure I've come up with. Since RTEMS
> doesn't support dynamic linking, I'm going to duplicate lines 3 to 19
> (___run_tc_abs_1) and 23 to 29 for each test case which leads to having a
> very big source file for testcase_executer.cpp.
> Is there any better solution that avoids code redundancy that I'm not aware
> of? Do you have advices on how to change this structure to minimize code
> redundancy? Any help is much appreciated.
> Note: ___TC_abs_1 is already defined in a separate CPP file.
>
> Thanks for your time.
>
> Kind Regards,
> SAeeD
>
> _______________________________________________
> users mailing list
> users at rtems.org
> http://lists.rtems.org/mailman/listinfo/users



More information about the users mailing list