aboutsummaryrefslogtreecommitdiff
path: root/tests/test_strfmt.c
blob: c8ab08e7e42edc6f645b0dab0d4adc1dd594b095 (plain) (blame)
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
#include <string.h>
#define USE_SECURE_MEM
#include "../src/extlib.h"
#include "stdbool.h"

int
test_strfmt ()
{
  int errno = 0;
  char *test_string_1 = strfmt ("meow", (struct style) {0, false, BOLD});
  printf ("\e[1mtest_strfmt\e[0m:: No Color, Bold:           %s\n", test_string_1);
  if (strcmp (test_string_1, "\e[1mmeow\e[0m") != 0)
    errno = 1;

  char *test_string_2 = strfmt ("meow", (struct style) {RED, false, 0});
  printf ("\e[1mtest_strfmt\e[0m:: Red, No style:            %s\n", test_string_2);
  if (strcmp (test_string_2, "\e[38;5;1mmeow\e[0m") != 0)
    errno = 2;

  char *test_string_3 = strfmt ("meow", (struct style) {RED, false, BOLD});
  printf ("\e[1mtest_strfmt\e[0m:: Red, Bold:                %s\n", test_string_3);
  if (strcmp (test_string_3, "\e[38;5;1m\e[1mmeow\e[0m") != 0)
    errno = 3;

  char *test_string_4 = strfmt ("meow", (struct style) {RED, true, 0});
  printf ("\e[1mtest_strfmt\e[0m:: Red background, No style: %s\n", test_string_4);
  if (strcmp (test_string_4, "\e[48;5;1mmeow\e[0m") != 0)
    errno = 4;

  char *test_string_5 = strfmt ("meow", (struct style) {RED, true, BOLD});
  printf ("\e[1mtest_strfmt\e[0m:: Red background, Bold:     %s\n", test_string_5);
  if (strcmp (test_string_5, "\e[48;5;1m\e[1mmeow\e[0m") != 0)
    errno = 5;

  free_secure ((void **) &test_string_1, strlen (test_string_1));
  free_secure ((void **) &test_string_2, strlen (test_string_2));
  free_secure ((void **) &test_string_3, strlen (test_string_3));
  free_secure ((void **) &test_string_4, strlen (test_string_4));
  free_secure ((void **) &test_string_5, strlen (test_string_5));
  return errno;
}

struct test_t*
test_strfmt_t ()
{
  struct test_t* test = malloc (sizeof (struct test_t));
  test->test_func=test_strfmt;
  test->test_name="test_strfmt";
  test->test_desc="strfmt";
  return test;
}