ext/dl/test/test.c


DEFINITIONS

This source file includes following functions.
  1. test_get_long_value
  2. test_set_long_value
  3. test_fill_test_struct
  4. test_fill_test_union
  5. test_alloc_test_struct
  6. test_c2i
  7. test_i2c
  8. test_lcc
  9. test_f2d
  10. test_d2f
  11. test_strlen
  12. test_isucc
  13. test_lsucc
  14. test_succ
  15. test_strcat
  16. test_arylen
  17. test_append
  18. test_init
  19. test_open
  20. test_close
  21. test_gets
  22. test_callback1
  23. test_call_func1
  24. test_data_init
  25. test_data_add
  26. test_data_aref


   1  #include <stdio.h>
   2  #include <string.h>
   3  
   4  static char internal_string[] = "internal_string";
   5  long internal_long_value = 100;
   6  
   7  struct test_struct {
   8    char c;
   9    long l;
  10  };
  11  
  12  union test_union {
  13    char c;
  14    int  i;
  15    long l;
  16    void *p;
  17  };
  18  
  19  struct test_data {
  20    char name[1024];
  21    struct test_data *next;
  22  };
  23  
  24  long
  25  test_get_long_value()
  26  {
  27    return internal_long_value;
  28  };
  29  
  30  void
  31  test_set_long_value(long l)
  32  {
  33    internal_long_value = l;
  34  };
  35  
  36  void
  37  test_fill_test_struct(struct test_struct *ptr, char c, long l)
  38  {
  39    ptr->c = c;
  40    ptr->l = l;
  41  };
  42  
  43  void
  44  test_fill_test_union(union test_union *ptr, long l)
  45  {
  46    ptr->l = l;
  47  };
  48  
  49  struct test_struct *
  50  test_alloc_test_struct(char c, long l)
  51  {
  52    struct test_struct *data;
  53  
  54    data = (struct test_struct *)malloc(sizeof(struct test_struct));
  55    data->c = c;
  56    data->l = l;
  57  
  58    return data;
  59  };
  60  
  61  int
  62  test_c2i(char c)
  63  {
  64    return (int)c;
  65  };
  66  
  67  char
  68  test_i2c(int i)
  69  {
  70    return (char)i;
  71  };
  72  
  73  long
  74  test_lcc(char c1, char c2)
  75  {
  76    return (long)(c1 + c2);
  77  };
  78  
  79  double
  80  test_f2d(float f)
  81  {
  82    double d;
  83    d = f;
  84    return d;
  85  };
  86  
  87  float
  88  test_d2f(double d)
  89  {
  90    float f;
  91    f = d;
  92    return f;
  93  };
  94  
  95  int
  96  test_strlen(const char *str)
  97  {
  98    return strlen(str);
  99  };
 100  
 101  int
 102  test_isucc(int i)
 103  {
 104    return (i+1);
 105  };
 106  
 107  long
 108  test_lsucc(long l)
 109  {
 110    return (l+1);
 111  };
 112  
 113  void
 114  test_succ(long *l)
 115  {
 116    (*l)++;
 117  };
 118  
 119  char *
 120  test_strcat(char *str1, const char *str2)
 121  {
 122    return strcat(str1, str2);
 123  };
 124  
 125  int
 126  test_arylen(char *ary[])
 127  {
 128    int i;
 129    for( i=0; ary[i]; i++ ){};
 130    return i;
 131  };
 132  
 133  void
 134  test_append(char *ary[], int len, char *astr)
 135  {
 136    int i;
 137    int size1,size2;
 138    char *str;
 139  
 140    size2 = strlen(astr);
 141  
 142    for( i=0; i <= len - 1; i++ ){
 143      size1 = strlen(ary[i]);
 144      str = (char*)malloc(size1 + size2 + 1);
 145      strcpy(str, ary[i]);
 146      strcat(str, astr);
 147      ary[i] = str;
 148    };
 149  };
 150  
 151  int
 152  test_init(int *argc, char **argv[])
 153  {
 154    int i;
 155    char s[256];
 156  
 157    for( i=0; i < (*argc); i++ ){
 158      sprintf(s, "arg%d", i);
 159      if( strcmp((*argv)[i], s) != 0 ){
 160        return 1;
 161      }
 162    }
 163    return 0;
 164  }
 165  
 166  FILE *
 167  test_open(const char *filename, const char *mode)
 168  {
 169    FILE *file;
 170    file = fopen(filename,mode);
 171    return file;
 172  };
 173  
 174  void
 175  test_close(FILE *file)
 176  {
 177    fclose(file);
 178  };
 179  
 180  char *
 181  test_gets(char *s, int size, FILE *f)
 182  {
 183    return fgets(s,size,f);
 184  };
 185  
 186  typedef int callback1_t(int, char *);
 187  #define CALLBACK_MSG "callback message"
 188  
 189  int
 190  test_callback1(int err, const char *msg)
 191  {
 192    if( strcmp(msg, CALLBACK_MSG) == 0 ){
 193      return 1;
 194    }
 195    else{
 196      return 0;
 197    }
 198  }
 199  
 200  int
 201  test_call_func1(callback1_t *func)
 202  {
 203    if( func ){
 204      return (*func)(0, CALLBACK_MSG);
 205    }
 206    else{
 207      return 0;
 208    }
 209  }
 210  
 211  struct test_data *
 212  test_data_init()
 213  {
 214    struct test_data *data;
 215  
 216    data = (struct test_data *)malloc(sizeof(struct test_data));
 217    data->next = NULL;
 218    memset(data->name, 0, 1024);
 219  
 220    return data;
 221  };
 222  
 223  void
 224  test_data_add(struct test_data *list, const char *name)
 225  {
 226    struct test_data *data;
 227  
 228    data = (struct test_data *)malloc(sizeof(struct test_data));
 229    memset(data->name, 0, 1024);
 230    strncpy(data->name, name, 1024);
 231    data->next = list->next;
 232    list->next = data;
 233  };
 234  
 235  struct test_data *
 236  test_data_aref(struct test_data *list, int i)
 237  {
 238    struct test_data *data;
 239    int j;
 240  
 241    for( data = list->next, j=0; data; data = data->next, j++ ){
 242      if( i == j ){
 243        return data;
 244      };
 245    };
 246    return NULL;
 247  };