aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/test_strjoin.c
blob: 023f84d24a381c4062247c7b5794ca7d4e50a1eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "libft.h"
#include "test_utils.h"

_S_CRASH (ft_strjoin_null_s1, ft_strjoin (NULL, "hello"))
_S_CRASH (ft_strjoin_null_s2, ft_strjoin ("hello", NULL))
_S_CRASH (ft_strjoin_null_both, ft_strjoin (NULL, NULL))

static void
_s_test_strjoin (void)
{
  int i;
  char label[128];

  _s_section ("ft_strjoin");

  /* NULL crashes */
  _s_check ("NULL s1 crashes", _s_crashes (_s_crash_ft_strjoin_null_s1));
  _s_check ("NULL s2 crashes", _s_crashes (_s_crash_ft_strjoin_null_s2));
  _s_check ("NULL both crashes", _s_crashes (_s_crash_ft_strjoin_null_both));

  /* two normal strings */
  {
    char *p = ft_strjoin ("hello ", "world");
    _s_check ("basic", p && strcmp (p, "hello world") == 0);
    free (p);
  }

  /* first string empty */
  {
    char *p = ft_strjoin ("", "world");
    _s_check ("s1 empty", p && strcmp (p, "world") == 0);
    free (p);
  }

  /* second string empty */
  {
    char *p = ft_strjoin ("hello", "");
    _s_check ("s2 empty", p && strcmp (p, "hello") == 0);
    free (p);
  }

  /* both empty */
  {
    char *p = ft_strjoin ("", "");
    _s_check ("both empty", p && strcmp (p, "") == 0);
    free (p);
  }

  /* returns independent copy */
  {
    char s1[] = "abc";
    char s2[] = "def";
    char *p = ft_strjoin (s1, s2);
    _s_check ("independent ptr", p && p != s1 && p != s2);
    s1[0] = 'X';
    _s_check ("mutation safe", p[0] == 'a');
    free (p);
  }

  /* randomized */
  for (i = 0; i < _S_RAND_ITERS; i++)
    {
      int len1 = rand () % 200 + 1;
      int len2 = rand () % 200 + 1;
      char *s1 = malloc (len1 + 1);
      char *s2 = malloc (len2 + 1);
      char *expected;
      char *p;
      int j;

      if (!s1 || !s2)
        {
          free (s1);
          free (s2);
          continue;
        }
      for (j = 0; j < len1; j++)
        s1[j] = 'A' + rand () % 26;
      s1[len1] = '\0';
      for (j = 0; j < len2; j++)
        s2[j] = 'a' + rand () % 26;
      s2[len2] = '\0';

      expected = malloc (len1 + len2 + 1);
      if (!expected)
        {
          free (s1);
          free (s2);
          continue;
        }
      memcpy (expected, s1, len1);
      memcpy (expected + len1, s2, len2 + 1);

      p = ft_strjoin (s1, s2);
      snprintf (label, sizeof (label), "random len1=%d len2=%d", len1, len2);
      _s_check (label, p && strcmp (p, expected) == 0);
      free (s1);
      free (s2);
      free (expected);
      free (p);
    }
}

int
main (void)
{
  srand (time (NULL));
  _s_test_strjoin ();
  _s_print_results ();
  return (_s_fail != 0);
}