1

I've to check one byte in 200 tables. I don't want to write 200 line to check it. Do you have any tips to do it with a for loop?

My example of code :

#include <stdio.h>
#include "CUnit/Basic.h"
#include "CUnit/Console.h"
#include "CUnit/Automated.h"
#include "CUnit/CUCurses.h"  
#include "CUnit/TestDB.h"

void _testu(){
  CU_ASSERT_EQUAL(55, my_tab_0[5]);
  CU_ASSERT_EQUAL(55, my_tab_1[5]);
  CU_ASSERT_EQUAL(55, my_tab_2[5]);
  CU_ASSERT_EQUAL(55, my_tab_3[5]);
   .
   .
   .
  CU_ASSERT_EQUAL(55, my_tab_n[5]);
}
Jguillot
  • 214
  • 3
  • 14

2 Answers2

3

If you have n independant arrays, you could try to build an array of pointers:

char *my_tab[] = { my_tab_0, my_tab_1, m__tab_2, ... my_tab_n };

you can they simply do:

void _testu(){
    int i;
    for(i=0; i<=n; i++) {
        CU_ASSERT_EQUAL(55, my_tab[i][5]);
    }
}

It mainly make sense if you have more than one loop using it.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • definitely cleaner than my answer +1 – fr_andres Oct 14 '16 at 10:31
  • you need anyway to hardcode the `my_tab_` entries, which the editor may still be good for – fr_andres Oct 14 '16 at 10:32
  • Do you know how can i recuperate the size of my_tab_0? `sizeof(my_tab[0])` doesn't give me the correct one – Jguillot Oct 14 '16 at 13:37
  • Jguillot: `my_tab[0]` is a mere pointer. In the initialization of `my_tab`, the array `my_tab__0` *decayed* to a pointer, and the size was lost. If the sizes can change from one array to the other, you will have to save them in another array. – Serge Ballesta Oct 14 '16 at 14:11
1

Since you can't change the datastructure, consider using advanced text editor tools to write those lines for you. It is not the most orthodox way to deal with it, but it works and it's at least fast and clear. In emacs, I would consider two options:

1: write a loop that generates the formatted string for you:

(defun copy_iter (strng times)
  (let ((result))
    (dotimes (i times result) 
      (setq result (concat result "\n" (format strng i))))))

And calling (copy_iter "CU_ASSERT_EQUAL(55, my_tab_%s[5]);" 200) will return what you expect

2: copy a generic string 200 times and then replace a marker using regular expressions:

1) copy the string with a generic marker 200 times (here is how):

"
CU_ASSERT_EQUAL(55, my_tab_<CHANGE_THIS>[5]);
CU_ASSERT_EQUAL(55, my_tab_<CHANGE_THIS>[5]);
CU_ASSERT_EQUAL(55, my_tab_<CHANGE_THIS>[5]);
CU_ASSERT_EQUAL(55, my_tab_<CHANGE_THIS>[5]);
CU_ASSERT_EQUAL(55, my_tab_<CHANGE_THIS>[5]);
...

2) call replace-regexp, with input: <CHANGE_THIS> and output: \# will also left the expected string, as explained here.

Of course, emacs is just a suggestion. The main point of my answer is to show that sometimes (or rather many times) the text editor itself can help us overcome such problems. I hope it helps!

Cheers

Community
  • 1
  • 1
fr_andres
  • 5,927
  • 2
  • 31
  • 44
  • i don't want to "copy/paste" my code, but i really want to implement a loop that check my byte but i take into account your answer for my futur projetcs ! – Jguillot Oct 14 '16 at 11:39
  • you pretty much need to do it anyway following the accepted answer, which I admit is much cleaner (iff all your arrays have the same size) – fr_andres Oct 15 '16 at 08:16