LCOV - code coverage report
Current view: top level - refarray - ref_array_ut.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 282 417 67.6 %
Date: 2014-04-01 Functions: 8 8 100.0 %

          Line data    Source code
       1             : /*
       2             :     REF ARRAY
       3             : 
       4             :     Implementation of the dynamic array with reference count.
       5             : 
       6             :     Copyright (C) Dmitri Pal <dpal@redhat.com> 2009
       7             : 
       8             :     This program is free software; you can redistribute it and/or modify
       9             :     it under the terms of the GNU General Public License as published by
      10             :     the Free Software Foundation; either version 3 of the License, or
      11             :     (at your option) any later version.
      12             :     This program is distributed in the hope that it will be useful,
      13             :     but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15             :     GNU General Public License for more details.
      16             :     You should have received a copy of the GNU General Public License
      17             :     along with this program.  If not, see <http://www.gnu.org/licenses/>.
      18             : */
      19             : 
      20             : #include "config.h"
      21             : #include <errno.h>  /* for errors */
      22             : #include <stdint.h>
      23             : #include <stdio.h>
      24             : #include <string.h>
      25             : #include <stdlib.h>
      26             : 
      27             : #include "ref_array.h"
      28             : #define TRACE_HOME
      29             : #include "trace.h"
      30             : 
      31             : int verbose = 0;
      32             : 
      33             : #define RAOUT(foo) \
      34             :     do { \
      35             :         if (verbose) foo; \
      36             :     } while(0)
      37             : 
      38             : typedef int (*test_fn)(void);
      39             : 
      40             : /* Basic test */
      41           1 : static int ref_array_basic_test(void)
      42             : {
      43           1 :     const char *line1 = "line1";
      44           1 :     const char *line2 = "line2";
      45           1 :     const char *line3 = "line3";
      46           1 :     const char *line4 = "line4";
      47           1 :     const char *line5 = "line5";
      48           1 :     const char *line6 = "line6";
      49             :     uint32_t i;
      50             :     struct ref_array *ra;
      51             :     struct ref_array *ra2;
      52           1 :     int error = EOK;
      53           1 :     uint32_t len = 0;
      54           1 :     uint32_t other_len = 0;
      55             :     char *ret;
      56             :     char *elem;
      57             :     void *ptr;
      58             : 
      59           1 :     error = ref_array_create(&ra, sizeof(char *), 1, NULL, NULL);
      60           1 :     if (error) {
      61           0 :         printf("Failed to create array %d\n", error);
      62           0 :         return error;
      63             :     }
      64             : 
      65           1 :     RAOUT(ref_array_debug(ra, 0));
      66             : 
      67           1 :     error = ref_array_append(ra, &line1);
      68           1 :     if (error) {
      69           0 :         ref_array_destroy(ra);
      70           0 :         printf("Failed to append to array line 1 %d\n", error);
      71           0 :         return error;
      72             :     }
      73             : 
      74           1 :     RAOUT(ref_array_debug(ra, 0));
      75             : 
      76           1 :     error = ref_array_append(ra, &line2);
      77           1 :     if (error) {
      78           0 :         ref_array_destroy(ra);
      79           0 :         printf("Failed to append to array line 2 %d\n", error);
      80           0 :         return error;
      81             :     }
      82             : 
      83           1 :     RAOUT(ref_array_debug(ra, 0));
      84             : 
      85           1 :     error = ref_array_append(ra, &line3);
      86           1 :     if (error) {
      87           0 :         ref_array_destroy(ra);
      88           0 :         printf("Failed to append to array line 3 %d\n", error);
      89           0 :         return error;
      90             :     }
      91             : 
      92           1 :     RAOUT(ref_array_debug(ra, 0));
      93             : 
      94           1 :     error = ref_array_append(ra, &line4);
      95           1 :     if (error) {
      96           0 :         ref_array_destroy(ra);
      97           0 :         printf("Failed to append to array line 4 %d\n", error);
      98           0 :         return error;
      99             :     }
     100             : 
     101           1 :     RAOUT(ref_array_debug(ra, 0));
     102             : 
     103           1 :     error = ref_array_append(ra, &line5);
     104           1 :     if (error) {
     105           0 :         ref_array_destroy(ra);
     106           0 :         printf("Failed to append to array line 5 %d\n", error);
     107           0 :         return error;
     108             :     }
     109             : 
     110           1 :     RAOUT(ref_array_debug(ra, 0));
     111             : 
     112           1 :     error = ref_array_append(ra, &line6);
     113           1 :     if (error) {
     114           0 :         ref_array_destroy(ra);
     115           0 :         printf("Failed to append to array line 6 %d\n", error);
     116           0 :         return error;
     117             :     }
     118             : 
     119           1 :     RAOUT(ref_array_debug(ra, 0));
     120             : 
     121           1 :     RAOUT(printf("\n\nTest 1 - Printing lines.\n\n"));
     122             : 
     123           1 :     error = ref_array_getlen(ra, &other_len);
     124           1 :     if (error) {
     125           0 :         ref_array_destroy(ra);
     126           0 :         printf("Failed to get length %d\n", error);
     127           0 :         return error;
     128             :     }
     129             : 
     130           1 :     len = ref_array_len(ra);
     131             : 
     132           1 :     if (len != other_len) {
     133           0 :         ref_array_destroy(ra);
     134           0 :         printf("Lengths do not match:\n");
     135           0 :         printf("Len    : %d\n", len);
     136           0 :         printf("Get Len: %d\n", other_len);
     137           0 :         return EFAULT;
     138             :     }
     139             : 
     140           6 :     for (i = 0; i < len; i++) {
     141           6 :         ref_array_get(ra, i, &ret);
     142           6 :         RAOUT(printf("%s\n", ret));
     143             :     }
     144             : 
     145           1 :     RAOUT(printf("\n\nTest 2 - Creating reference and then printing lines.\n\n"));
     146             : 
     147           1 :     ra2 = ref_array_getref(ra);
     148           1 :     ref_array_destroy(ra);
     149             : 
     150           7 :     for (i = 0; i < len; i++) {
     151           6 :         ret = *((char **)ref_array_get(ra2, i, NULL));
     152           6 :         RAOUT(printf("%s\n", ret));
     153             :     }
     154             : 
     155           1 :     RAOUT(printf("\n\nTest 3 - Get elements with copying.\n\n"));
     156             : 
     157           6 :     for (i = 0; i < len; i++) {
     158           6 :         ref_array_get(ra2, i, &ret);
     159           6 :         RAOUT(printf("%s\n", ret));
     160             :     }
     161             : 
     162           1 :     RAOUT(printf("\n\nTest 4a - Get elements with copying and assignment.\n\n"));
     163             : 
     164             :     /* This is a bad practice to use one variable
     165             :      * as a parameter and as an acceptor for the return value.
     166             :      * See next example for a better way to do it.
     167             :      */
     168           6 :     for (i = 0; i < len; i++) {
     169           6 :         ret = *((char **)ref_array_get(ra2, i, &ret));
     170           6 :         RAOUT(printf("%s\n", ret));
     171             :     }
     172             : 
     173           1 :     RAOUT(printf("\n\nTest 4b - Get elements with copying and assignment.\n\n"));
     174             : 
     175           6 :     for (i = 0; i < len; i++) {
     176           6 :         ret = *((char **)ref_array_get(ra2, i, &elem));
     177           6 :         RAOUT(printf("%s\n", ret));
     178           6 :         RAOUT(printf("%s\n", elem));
     179           6 :         if (strcmp(ret, elem) != 0) {
     180           0 :             ref_array_destroy(ra2);
     181           0 :             printf("\nRetrieved strings were expected to be same,\n");
     182           0 :             printf("but they are not:\n");
     183           0 :             printf("By pointer:[%s]\nAs element:[%s]\n", ret, elem);
     184           0 :             return EFAULT;
     185             :         }
     186             :     }
     187             : 
     188           1 :     RAOUT(printf("\n\nTest 5 - While loop up.\n\n"));
     189             : 
     190             :     i = 0;
     191             :     for (;;) {
     192           7 :         ptr = ref_array_get(ra2, i, &ret);
     193           7 :         if (ptr) {
     194           6 :             RAOUT(printf("%s\n", ret));
     195           6 :             i++;
     196             :         }
     197             :         else break;
     198           6 :     }
     199             : 
     200           1 :     RAOUT(printf("\n\nTest 6 - While loop down.\n\n"));
     201             : 
     202           1 :     i = len - 1;
     203             :     for (;;) {
     204           7 :         ptr = ref_array_get(ra2, i, &ret);
     205           7 :         if (ptr) {
     206           6 :             RAOUT(printf("%s\n", ret));
     207           6 :             i--;
     208             :         }
     209             :         else break;
     210           6 :     }
     211             : 
     212           1 :     RAOUT(printf("\n\nDone!!!\n\n"));
     213             : 
     214           1 :     ref_array_destroy(ra2);
     215           1 :     return EOK;
     216             : }
     217             : 
     218          40 : static void array_cleanup(void *elem,
     219             :                           ref_array_del_enum type,
     220             :                           void *data)
     221             : {
     222          40 :     RAOUT(printf("%s%s\n", (char *)data, *((char **)elem)));
     223          40 :     free(*((char **)elem));
     224          40 : }
     225             : 
     226             : /* Free test */
     227           1 : static int ref_array_free_test(void)
     228             : {
     229           1 :     const char *line1 = "line1";
     230           1 :     const char *line2 = "line2";
     231           1 :     const char *line3 = "line3";
     232           1 :     const char *line4 = "line4";
     233           1 :     char text[] = "Deleting: ";
     234             :     char *str;
     235             :     uint32_t i;
     236             :     struct ref_array *ra;
     237           1 :     int error = EOK;
     238             :     char *ret;
     239             :     void *ptr;
     240             : 
     241           1 :     error = ref_array_create(&ra, sizeof(char *), 1, array_cleanup, (char *)text);
     242           1 :     if (error) {
     243           0 :         printf("Failed to create array %d\n", error);
     244           0 :         return error;
     245             :     }
     246             : 
     247           1 :     RAOUT(ref_array_debug(ra, 0));
     248             : 
     249           1 :     str = strdup(line1);
     250             : 
     251           1 :     error = ref_array_append(ra, &str);
     252           1 :     if (error) {
     253           0 :         ref_array_destroy(ra);
     254           0 :         printf("Failed to append to array line 1 %d\n", error);
     255           0 :         return error;
     256             :     }
     257             : 
     258           1 :     RAOUT(ref_array_debug(ra, 0));
     259             : 
     260           1 :     str = strdup(line2);
     261             : 
     262           1 :     error = ref_array_append(ra, &str);
     263           1 :     if (error) {
     264           0 :         ref_array_destroy(ra);
     265           0 :         printf("Failed to append to array line 2 %d\n", error);
     266           0 :         return error;
     267             :     }
     268             : 
     269           1 :     RAOUT(ref_array_debug(ra, 0));
     270             : 
     271           1 :     str = strdup(line3);
     272             : 
     273           1 :     error = ref_array_append(ra, &str);
     274           1 :     if (error) {
     275           0 :         ref_array_destroy(ra);
     276           0 :         printf("Failed to append to array line 3 %d\n", error);
     277           0 :         return error;
     278             :     }
     279             : 
     280           1 :     RAOUT(ref_array_debug(ra, 0));
     281             : 
     282           1 :     str = strdup(line4);
     283             : 
     284           1 :     error = ref_array_append(ra, &str);
     285           1 :     if (error) {
     286           0 :         ref_array_destroy(ra);
     287           0 :         printf("Failed to append to array line 4 %d\n", error);
     288           0 :         return error;
     289             :     }
     290             : 
     291           1 :     RAOUT(ref_array_debug(ra, 0));
     292             : 
     293             :     i = 0;
     294             :     for (;;) {
     295           5 :         ptr = ref_array_get(ra, i, &ret);
     296           5 :         if (ptr) {
     297           4 :             RAOUT(printf("%s\n", ret));
     298           4 :             i++;
     299             :         }
     300             :         else break;
     301           4 :     }
     302             : 
     303           1 :     RAOUT(printf("\n\nDone!!!\n\n"));
     304             : 
     305           1 :     ref_array_destroy(ra);
     306           1 :     return EOK;
     307             : }
     308             : 
     309           1 : static int ref_array_adv_test(void)
     310             : {
     311           1 :     int error = EOK;
     312           1 :     const char *lines[] = { "line0",
     313             :                             "line1",
     314             :                             "line2",
     315             :                             "line3",
     316             :                             "line4",
     317             :                             "line5",
     318             :                             "line6",
     319             :                             "line7",
     320             :                             "line8",
     321             :                             "line9" };
     322           1 :     char text[] = "Deleting: ";
     323             :     char *str;
     324             :     uint32_t i;
     325             :     struct ref_array *ra;
     326             :     char *ret;
     327             :     void *ptr;
     328           1 :     int expected[] = { 0, 1, 7, 8, 9 };
     329           1 :     int expected2[] = { 1, 7, 8, 9, 0 };
     330             : 
     331           1 :     error = ref_array_create(&ra,
     332             :                              sizeof(char *),
     333             :                              1,
     334             :                              array_cleanup,
     335             :                              (char *)text);
     336           1 :     if (error) {
     337           0 :         printf("Failed to create array %d\n", error);
     338           0 :         return error;
     339             :     }
     340             : 
     341           5 :     for (i = 0; i < 5;i++) {
     342             : 
     343           5 :         str = strdup(lines[i]);
     344             : 
     345           5 :         error = ref_array_append(ra, &str);
     346           5 :         if (error) {
     347           0 :             ref_array_destroy(ra);
     348           0 :             printf("Failed to append line %d, error %d\n",
     349             :                     i, error);
     350           0 :             return error;
     351             :         }
     352             :     }
     353             : 
     354           1 :     RAOUT(printf("\nInitial array.\n"));
     355             : 
     356             :     i = 0;
     357             :     for (;;) {
     358           6 :         ptr = ref_array_get(ra, i, &ret);
     359           6 :         if (ptr) {
     360           5 :             RAOUT(printf("%s\n", ret));
     361           5 :             i++;
     362             :         }
     363             :         else break;
     364           5 :     }
     365             : 
     366             : 
     367             :     /* Try to remove invalid entry */
     368           1 :     error = ref_array_remove(ra, 1000);
     369           1 :     if (error != ERANGE) {
     370           0 :         ref_array_destroy(ra);
     371           0 :         printf("Removing entry expected error got success.\n");
     372           0 :         return -1;
     373             :     }
     374             : 
     375             :     /* Try to insert invalid entry */
     376           1 :     error = ref_array_insert(ra, 1000, &text);
     377           1 :     if (error != ERANGE) {
     378           0 :         ref_array_destroy(ra);
     379           0 :         printf("Inserting entry expected error got success.\n");
     380           0 :         return -1;
     381             :     }
     382             : 
     383             :     /* Try to replace invalid entry */
     384           1 :     error = ref_array_replace(ra, 1000, &text);
     385           1 :     if (error != ERANGE) {
     386           0 :         ref_array_destroy(ra);
     387           0 :         printf("Replacing entry expected error got success.\n");
     388           0 :         return -1;
     389             :     }
     390             : 
     391             :     /* Insert several entries */
     392           5 :     for (i = 9; i > 4; i--) {
     393             : 
     394           5 :         str = strdup(lines[i]);
     395             : 
     396           5 :         error = ref_array_insert(ra, 9 - i, &str);
     397           5 :         if (error) {
     398           0 :             ref_array_destroy(ra);
     399           0 :             free(str);
     400           0 :             printf("Failed to insert line %d, error %d\n",
     401             :                     i, error);
     402           0 :             return error;
     403             :         }
     404             :     }
     405             : 
     406             :     /* Displpay array contents */
     407           1 :     RAOUT(printf("\nArray with inserted values.\n"));
     408             :     i = 0;
     409             :     for (;;) {
     410          11 :         ptr = ref_array_get(ra, i, &ret);
     411          11 :         if (ptr) {
     412          10 :             RAOUT(printf("%s\n", ret));
     413          10 :             i++;
     414             :         }
     415             :         else break;
     416          10 :     }
     417             : 
     418             :     /* Replace everything */
     419          10 :     for (i = 0; i < 10;i++) {
     420             : 
     421          10 :         str = strdup(lines[i]);
     422             : 
     423          10 :         error = ref_array_replace(ra, i, &str);
     424          10 :         if (error) {
     425           0 :             ref_array_destroy(ra);
     426           0 :             free(str);
     427           0 :             printf("Failed to replace line %d, error %d\n",
     428             :                     i, error);
     429           0 :             return error;
     430             :         }
     431             :     }
     432             : 
     433             :     /* Displpay array contents */
     434           1 :     RAOUT(printf("\nArray with replaced values.\n"));
     435             :     i = 0;
     436             :     for (;;) {
     437          11 :         ptr = ref_array_get(ra, i, &ret);
     438          11 :         if (ptr) {
     439          10 :             RAOUT(printf("%s\n", ret));
     440          10 :             i++;
     441             :         }
     442             :         else break;
     443          10 :     }
     444             : 
     445             :     /* Reset */
     446           1 :     ref_array_reset(ra);
     447             : 
     448             :     /* Displpay array contents */
     449           1 :     RAOUT(printf("\nEmpty array.\n"));
     450             :     i = 0;
     451             :     for (;;) {
     452           1 :         ptr = ref_array_get(ra, i, &ret);
     453           1 :         if (ptr) {
     454           0 :             RAOUT(printf("%s\n", ret));
     455           0 :             i++;
     456             :         }
     457             :         else break;
     458           0 :     }
     459             : 
     460             :     /* Add everything */
     461          10 :     for (i = 0; i < 10;i++) {
     462             : 
     463          10 :         str = strdup(lines[i]);
     464             : 
     465          10 :         error = ref_array_insert(ra, i, &str);
     466          10 :         if (error) {
     467           0 :             ref_array_destroy(ra);
     468           0 :             free(str);
     469           0 :             printf("Failed to insert into array %d\n", error);
     470           0 :             return error;
     471             :         }
     472             :     }
     473             : 
     474             :     /* Displpay array contents */
     475           1 :     RAOUT(printf("\nAll added back.\n"));
     476             :     i = 0;
     477             :     for (;;) {
     478          11 :         ptr = ref_array_get(ra, i, &ret);
     479          11 :         if (ptr) {
     480          10 :             RAOUT(printf("%s\n", ret));
     481          10 :             i++;
     482             :         }
     483             :         else break;
     484          10 :     }
     485             : 
     486             :     /* Remove part */
     487           5 :     for (i = 0; i < 5;i++) {
     488             : 
     489           5 :         error = ref_array_remove(ra, 2);
     490           5 :         if (error) {
     491           0 :             ref_array_destroy(ra);
     492           0 :             printf("Failed to remive item from array %d\n", error);
     493           0 :             return error;
     494             :         }
     495             :     }
     496             : 
     497             :     /* Displpay array contents */
     498           1 :     RAOUT(printf("\nCleaned array.\n"));
     499             :     i = 0;
     500             :     for (;;) {
     501           6 :         ptr = ref_array_get(ra, i, &ret);
     502           6 :         if (ptr) {
     503           5 :             RAOUT(printf("%s\n", ret));
     504           5 :             i++;
     505             :         }
     506             :         else break;
     507           5 :     }
     508             : 
     509           1 :     RAOUT(printf("\n\nChecking for expected contents\n\n"));
     510             : 
     511             :     i = 0;
     512             :     for (;;) {
     513           6 :         ptr = ref_array_get(ra, i, &ret);
     514           6 :         if (ptr) {
     515           5 :             RAOUT(printf("Comparing:\n[%s]\n[%s]\n\n",
     516             :                   ret, lines[expected[i]]));
     517           5 :             if (strcmp(ret, lines[expected[i]]) != 0) {
     518           0 :                 printf("Unexpected contents of the array.\n");
     519           0 :                 ref_array_destroy(ra);
     520           0 :                 return -1;
     521             :             }
     522           5 :             i++;
     523             :         }
     524             :         else break;
     525           5 :     }
     526             : 
     527           1 :     RAOUT(printf("\n\nSwap test\n\n"));
     528             : 
     529           2 :     if ((error = ref_array_swap(ra, 0, 1)) ||
     530           2 :         (error = ref_array_swap(ra, 1, 2)) ||
     531           2 :         (error = ref_array_swap(ra, 2, 3)) ||
     532           1 :         (error = ref_array_swap(ra, 3, 4))) {
     533           0 :         ref_array_destroy(ra);
     534           0 :         printf("Failed to to swap %d\n", error);
     535           0 :         return error;
     536             :     }
     537             : 
     538             :     i = 0;
     539             :     for (;;) {
     540           6 :         ptr = ref_array_get(ra, i, &ret);
     541           6 :         if (ptr) {
     542           5 :             RAOUT(printf("Comparing:\n[%s]\n[%s]\n\n",
     543             :                   ret, lines[expected2[i]]));
     544           5 :             if (strcmp(ret, lines[expected2[i]]) != 0) {
     545           0 :                 printf("Unexpected contents of the array.\n");
     546           0 :                 ref_array_destroy(ra);
     547           0 :                 return -1;
     548             :             }
     549           5 :             i++;
     550             :         }
     551             :         else break;
     552           5 :     }
     553             : 
     554           1 :     RAOUT(printf("\n\nDone!!!\n\n"));
     555             : 
     556           1 :     ref_array_destroy(ra);
     557           1 :     return EOK;
     558             : }
     559             : 
     560           6 : static int copy_cb(void *elem,
     561             :                    void *new_elem)
     562             : {
     563           6 :     char *ne = NULL;
     564             : 
     565           6 :     ne = strdup(*((char **)elem));
     566           6 :     *((char **)new_elem) = ne;
     567             : 
     568           6 :     RAOUT(printf("Source: %s\nCopy:%s\n", *((char **)elem), ne));
     569             : 
     570             : 
     571           6 :     return EOK;
     572             : }
     573             : 
     574           1 : static int ref_array_copy_test(void)
     575             : {
     576           1 :     const char *line1 = "line1";
     577           1 :     const char *line2 = "line2";
     578           1 :     const char *line3 = "line3";
     579           1 :     const char *line4 = "line4";
     580           1 :     const char *line5 = "line5";
     581           1 :     const char *line6 = "line6";
     582             :     uint32_t i;
     583             :     struct ref_array *ra;
     584             :     struct ref_array *ra2;
     585           1 :     int error = EOK;
     586           1 :     uint32_t len = 6;
     587           1 :     char text[] = "Deleting: ";
     588             : 
     589           1 :     error = ref_array_create(&ra, sizeof(char *), 1, NULL, NULL);
     590           1 :     if (error) {
     591           0 :         printf("Failed to create array %d\n", error);
     592           0 :         return error;
     593             :     }
     594             : 
     595           1 :     RAOUT(ref_array_debug(ra, 0));
     596             : 
     597           1 :     error = ref_array_append(ra, &line1);
     598           1 :     if (error) {
     599           0 :         ref_array_destroy(ra);
     600           0 :         printf("Failed to append to array line 1 %d\n", error);
     601           0 :         return error;
     602             :     }
     603             : 
     604           1 :     RAOUT(ref_array_debug(ra, 0));
     605             : 
     606           1 :     error = ref_array_append(ra, &line2);
     607           1 :     if (error) {
     608           0 :         ref_array_destroy(ra);
     609           0 :         printf("Failed to append to array line 2 %d\n", error);
     610           0 :         return error;
     611             :     }
     612             : 
     613           1 :     RAOUT(ref_array_debug(ra, 0));
     614             : 
     615           1 :     error = ref_array_append(ra, &line3);
     616           1 :     if (error) {
     617           0 :         ref_array_destroy(ra);
     618           0 :         printf("Failed to append to array line 3 %d\n", error);
     619           0 :         return error;
     620             :     }
     621             : 
     622           1 :     RAOUT(ref_array_debug(ra, 0));
     623             : 
     624           1 :     error = ref_array_append(ra, &line4);
     625           1 :     if (error) {
     626           0 :         ref_array_destroy(ra);
     627           0 :         printf("Failed to append to array line 4 %d\n", error);
     628           0 :         return error;
     629             :     }
     630             : 
     631           1 :     RAOUT(ref_array_debug(ra, 0));
     632             : 
     633           1 :     error = ref_array_append(ra, &line5);
     634           1 :     if (error) {
     635           0 :         ref_array_destroy(ra);
     636           0 :         printf("Failed to append to array line 5 %d\n", error);
     637           0 :         return error;
     638             :     }
     639             : 
     640           1 :     RAOUT(ref_array_debug(ra, 0));
     641             : 
     642           1 :     error = ref_array_append(ra, &line6);
     643           1 :     if (error) {
     644           0 :         ref_array_destroy(ra);
     645           0 :         printf("Failed to append to array line 6 %d\n", error);
     646           0 :         return error;
     647             :     }
     648             : 
     649           1 :     RAOUT(ref_array_debug(ra, 0));
     650             : 
     651           1 :     RAOUT(printf("\n\nCopy lines.\n\n"));
     652             : 
     653           1 :     error = ref_array_copy(ra, copy_cb, array_cleanup, (char *)text, &ra2);
     654           1 :     if (error) {
     655           0 :         ref_array_destroy(ra);
     656           0 :         printf("Failed to copy array %d\n", error);
     657           0 :         return error;
     658             :     }
     659             : 
     660           6 :     for (i = 0; i < len; i++) {
     661           6 :         if (strcmp(*((char **)ref_array_get(ra, i, NULL)),
     662             :                    *((char **)ref_array_get(ra2, i, NULL))) != 0) {
     663           0 :             printf("\nRetrieved strings were expected to be same,\n");
     664           0 :             printf("but they are not:\n");
     665           0 :             printf("First:[%s]\nSecond:[%s]\n",
     666           0 :                     *((char **)ref_array_get(ra, i, NULL)),
     667           0 :                     *((char **)ref_array_get(ra2, i, NULL)));
     668           0 :             ref_array_destroy(ra);
     669           0 :             ref_array_destroy(ra2);
     670           0 :             return EFAULT;
     671             :         }
     672             :     }
     673             : 
     674           1 :     RAOUT(printf("\n\nSource array.\n\n"));
     675           1 :     RAOUT(ref_array_debug(ra, 0));
     676           1 :     ref_array_destroy(ra);
     677             : 
     678           1 :     RAOUT(printf("\n\nAbout to destroy a copy.\n\n"));
     679           1 :     RAOUT(ref_array_debug(ra2, 0));
     680           1 :     ref_array_destroy(ra2);
     681             : 
     682           1 :     RAOUT(printf("\n\nDone!!!\n\n"));
     683             :     return EOK;
     684             : 
     685             : }
     686             : 
     687           1 : static int ref_array_copy_num_test(void)
     688             : {
     689             :     uint32_t i,j,k;
     690             :     struct ref_array *ra;
     691             :     struct ref_array *ra2;
     692           1 :     int error = EOK;
     693           1 :     uint32_t len = 5;
     694             : 
     695           1 :     error = ref_array_create(&ra, sizeof(uint32_t), 1, NULL, NULL);
     696           1 :     if (error) {
     697           0 :         printf("Failed to create array %d\n", error);
     698           0 :         return error;
     699             :     }
     700             : 
     701           1 :     RAOUT(ref_array_debug(ra, 1));
     702             : 
     703           6 :     for (i = 0; i < len; i++) {
     704           5 :         error = ref_array_append(ra, &i);
     705           5 :         if (error) {
     706           0 :             ref_array_destroy(ra);
     707           0 :             printf("Failed to append number to array %d\n", error);
     708           0 :             return error;
     709             :         }
     710             : 
     711           5 :         RAOUT(ref_array_debug(ra, 1));
     712             :     }
     713             : 
     714           1 :     RAOUT(printf("\n\nCopy num test.\n\n"));
     715             : 
     716           1 :     error = ref_array_copy(ra, NULL, NULL, NULL, &ra2);
     717           1 :     if (error) {
     718           0 :         ref_array_destroy(ra);
     719           0 :         printf("Failed to copy array %d\n", error);
     720           0 :         return error;
     721             :     }
     722             : 
     723           6 :     for (i = 0; i < len; i++) {
     724           5 :         j = *((uint32_t *)(ref_array_get(ra, i, NULL)));
     725           5 :         k = *((uint32_t *)(ref_array_get(ra2, i, NULL)));
     726           5 :         if (j != k) {
     727           0 :             printf("\nRetrieved values were expected to be same,\n");
     728           0 :             printf("but they are not:\n");
     729           0 :             printf("First:[%d]\nSecond:[%d]\n", j, k);
     730           0 :             ref_array_destroy(ra);
     731           0 :             ref_array_destroy(ra2);
     732           0 :             return EFAULT;
     733             :         }
     734             :     }
     735             : 
     736           1 :     ref_array_destroy(ra);
     737           1 :     ref_array_destroy(ra2);
     738             : 
     739           1 :     RAOUT(printf("\n\nDone!!!\n\n"));
     740             :     return EOK;
     741             : 
     742             : }
     743             : 
     744             : /* Main function of the unit test */
     745           1 : int main(int argc, char *argv[])
     746             : {
     747           1 :     int error = 0;
     748           1 :     test_fn tests[] = { ref_array_basic_test,
     749             :                         ref_array_free_test,
     750             :                         ref_array_adv_test,
     751             :                         ref_array_copy_test,
     752             :                         ref_array_copy_num_test,
     753             :                         NULL };
     754             :     test_fn t;
     755           1 :     int i = 0;
     756             :     char *var;
     757             : 
     758           1 :     if ((argc > 1) && (strcmp(argv[1], "-v") == 0)) verbose = 1;
     759             :     else {
     760           1 :         var = getenv("COMMON_TEST_VERBOSE");
     761           1 :         if (var) verbose = 1;
     762             :     }
     763             : 
     764           1 :     RAOUT(printf("Start\n"));
     765             : 
     766           6 :     while ((t = tests[i++])) {
     767           5 :         error = t();
     768           5 :         if (error) {
     769           0 :             RAOUT(printf("Failed with error %d!\n", error));
     770           0 :             return error;
     771             :         }
     772             :     }
     773             : 
     774           1 :     RAOUT(printf("Success!\n"));
     775             :     return 0;
     776             : }

Generated by: LCOV version 1.10