Advertisement
Guest User

libft_main_util

a guest
Apr 25th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.88 KB | None | 0 0
  1. /*================ Includes =================*/
  2.  
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <setjmp.h>
  6. #include <signal.h>
  7. #include <ctype.h>
  8. #include <stdio.h>
  9.  
  10. #include "libft.h"
  11.  
  12. /*================ Definitions =================*/
  13.  
  14. #define FALSE   0
  15. #define TRUE    1
  16.  
  17. #define C_RED     "\x1b[31m"
  18. #define C_GREEN   "\x1b[32m"
  19. #define C_YELLOW  "\x1b[33m"
  20. #define C_BLUE    "\x1b[34m"
  21. #define C_MAGENTA "\x1b[35m"
  22. #define C_CYAN    "\x1b[36m"
  23. #define RESET     "\x1b[0m"
  24.  
  25. #define MALLOC_LEN 12
  26.  
  27. /*================ Segfault handling =================*/
  28.  
  29. jmp_buf restore;
  30.  
  31. void segfault_handler(int sig, siginfo_t *info, void *ptr)
  32. {
  33.     if (sig == SIGSEGV)
  34.     {
  35.         longjmp(restore, SIGSEGV);
  36.     }
  37. }
  38.  
  39. /*================ Testing functions =================*/
  40.  
  41.  
  42. int bool_equals(int a, int b)
  43. {
  44.     return (a == 0) ? (b == 0) : (b != 0);
  45. }
  46.  
  47. int str_equals(char const *str1, char const *str2)
  48. {
  49.     size_t i;
  50.  
  51.     if (str1 == str2)
  52.         return (1);
  53.     if (str1 && str2)
  54.     {
  55.         i = 0;
  56.         while (str1[i] && str2[i])
  57.         {
  58.             if (str1[i] != str2[i])
  59.                 return (0);
  60.             ++i;
  61.         }
  62.         return (str1[0] == str2[0]);
  63.     }
  64.     return (0);
  65. }
  66.  
  67. char *print_memory(void const *ptr, size_t length)
  68. {
  69.     char *result;
  70.     t_u8 byte;
  71.     char hi;
  72.     char lo;
  73.  
  74.     if (ptr == NULL)
  75.         return (NULL);
  76.     if (!(result = (char *)malloc(length * 3)))
  77.         return (NULL);
  78.     for (size_t i = 0; i < length; ++i)
  79.     {
  80.         byte = ((t_u8 const *)ptr)[i];
  81.         hi = ((byte >> 4) & 0xF);
  82.         lo = (byte & 0xF);
  83.         result[i * 3 + 0] = hi + (hi < 10 ? '0' : 'A' - 10);
  84.         result[i * 3 + 1] = lo + (lo < 10 ? '0' : 'A' - 10);
  85.         result[i * 3 + 2] = (i + 1 == length) ? '\0' : ' ';
  86.     }
  87.     return (result);
  88. }
  89.  
  90.  
  91.  
  92. void    test_int(
  93.         char const *test_name,
  94.         char const *function,
  95.         t_u32 result,
  96.         t_u32 expect,
  97.         int can_segfault)
  98. {
  99.     if (test_name)
  100.     {
  101.         if (can_segfault)
  102.              printf("\n%s - "C_YELLOW"can segfault"RESET, test_name);
  103.         else printf("\n%s", test_name);
  104.         printf(" -> ");
  105.     }
  106.     else printf(", ");
  107.     if (result == expect)
  108.     {
  109.         printf(C_GREEN"OK!"RESET);
  110.     }
  111.     else
  112.     {
  113.         printf(C_RED"ERROR\n");
  114.         if (function[0] == '_')
  115.             printf("ft%s: %d\n Expected: %d"RESET, function,
  116.                 result,
  117.                 expect);
  118.         else printf("ft_%s: %d\n   %s: %d"RESET,
  119.                 function, result,
  120.                 function, expect);
  121.     }
  122. }
  123.  
  124. void    test_str(
  125.         char const *test_name,
  126.         char const *function,
  127.         char const *result,
  128.         char const *expect,
  129.         int can_segfault)
  130. {
  131.     if (test_name)
  132.     {
  133.         if (can_segfault)
  134.              printf("\n%s - "C_YELLOW"can segfault"RESET, test_name);
  135.         else printf("\n%s", test_name);
  136.         printf(" -> ");
  137.     }
  138.     else printf(", ");
  139.     if (str_equals(result, expect))
  140.     {
  141.         printf(C_GREEN"OK!"RESET);
  142.     }
  143.     else
  144.     {
  145.         if (str_equals(expect, "(n/a)"))
  146.              printf(C_RED"TEST COULD NOT BE PERFORMED\n"RESET);
  147.         else printf(C_RED"ERROR\n");
  148.         if (function[0] == '_')
  149.             printf("ft%s: {%s}\n Expected: {%s}"RESET, function,
  150.                 result,
  151.                 expect);
  152.         else printf("ft_%s: {%s}\n   %s: {%s}"RESET,
  153.                 function, result,
  154.                 function, expect);
  155.     }
  156. }
  157.  
  158. void    test_mem(
  159.         char const *test_name,
  160.         char const *function,
  161.         void const *result,
  162.         void const *expect,
  163.         size_t length,
  164.         int can_segfault)
  165. {
  166.     int error;
  167.     if (test_name)
  168.     {
  169.         if (can_segfault)
  170.              printf("\n%s - "C_YELLOW"can segfault"RESET, test_name);
  171.         else printf("\n%s", test_name);
  172.         printf(" -> ");
  173.     }
  174.     else printf(", ");
  175.     error = (result && expect) ?
  176.         (memcmp(result, expect, length) != 0) :
  177.         (result != expect);
  178.     if (!error)
  179.     {
  180.         printf(C_GREEN"OK!"RESET);
  181.     }
  182.     else
  183.     {
  184.         printf(C_RED"ERROR\n");
  185.         if (function[0] == '_')
  186.             printf("ft%s: {%s}\n Expected: {%s}"RESET, function,
  187.                 print_memory(result, length),
  188.                 print_memory(expect, length));
  189.         else printf("ft_%s: {%s}\n   %s: {%s}"RESET,
  190.                 function, print_memory(result, length),
  191.                 function, print_memory(expect, length));
  192.     }
  193. }
  194.  
  195. void    test_strarray(
  196.         char const *test_name,
  197.         char const *function,
  198.         char const **result,
  199.         char const **expect,
  200.         int can_segfault)
  201. {
  202.     if (test_name)
  203.     {
  204.         if (can_segfault)
  205.              printf("\n%s - "C_YELLOW"can segfault"RESET, test_name);
  206.         else printf("\n%s", test_name);
  207.         printf(" -> ");
  208.     }
  209.     else printf(", ");
  210.     int error = FALSE;
  211.     for (int i = 0; result[i] && expect[i]; ++i)
  212.     {
  213.         if (!str_equals(result[i], expect[i]))
  214.         {
  215.             error = TRUE;
  216.             break;
  217.         }
  218.     }
  219.     if (!error)
  220.     {
  221.         printf(C_GREEN"OK!"RESET);
  222.     }
  223.     else
  224.     {
  225.         printf(C_RED"ERROR\n");
  226.         printf("\nft_%s: [", function);
  227.         for (int i = 0; result[i]; ++i)
  228.             printf("%s%s", result[i], result[i + 1] ? ", " : "]");
  229.         printf("\n   Expected: [");
  230.         for (int i = 0; expect[i]; ++i)
  231.             printf("%s%s", expect[i], expect[i + 1] ? ", " : "]");
  232.     }
  233. }
  234.  
  235. void    test_lst(
  236.         char const *test_name,
  237.         char const *function,
  238.         t_list const *result,
  239.         char const *expect[],
  240.         int can_segfault)
  241. {
  242.     int error = FALSE;
  243.     if (test_name)
  244.     {
  245.         if (can_segfault)
  246.              printf("\n%s - "C_YELLOW"can segfault"RESET, test_name);
  247.         else printf("\n%s", test_name);
  248.         printf(" -> ");
  249.     }
  250.     else printf(", ");
  251.     t_list *lst = (t_list *)result;
  252.     t_u32 i = 0;
  253.     while (lst && expect[i])
  254.     {
  255.         if (!str_equals(lst->content, expect[i]))
  256.             error = TRUE;
  257.         lst = lst->next;
  258.         ++i;
  259.     }
  260.     if (lst || expect[i]) error = TRUE;
  261.     if (!error)
  262.     {
  263.         printf(C_GREEN"OK!"RESET);
  264.     }
  265.     else
  266.     {
  267.         printf(C_RED"ERROR\n");
  268.         lst = (t_list *)result;
  269.         printf(    "ft_%s: [", function);
  270.         while (lst)
  271.         {
  272.             printf("%s{%s}", (lst == result ? "" : ", "), lst->content);
  273.             lst = lst->next;
  274.         }
  275.         printf("]\n");
  276.         i = 0;
  277.         printf(" Expected: [", function);
  278.         while (expect[i])
  279.         {
  280.             printf("%s{%s}", (i == 0 ? "" : ", "), expect[i]);
  281.             ++i;
  282.         }
  283.         printf("]\n"RESET);
  284.     }
  285. }
  286.  
  287.  
  288.  
  289. /*================ Test utility functions =================*/
  290.  
  291. void    ptrtolower(char *c)
  292. {
  293.     *c = ft_tolower(*c);
  294. }
  295.  
  296. void    ptrtolower_1on2(t_u32 i, char *c)
  297. {
  298.     if (i % 2 == 0)
  299.         *c = ft_tolower(*c);
  300. }
  301.  
  302. char    toupper_chr(char c)
  303. {
  304.     if ('a' <= c && c <= 'z')
  305.         return (c - 'a' + 'A');
  306.     else return (c);
  307. }
  308.  
  309. char    toupper_1on2(t_u32 i, char c)
  310. {
  311.     if (i % 2)
  312.         c = ft_toupper(c);
  313.     return (c);
  314. }
  315.  
  316. /*
  317. Returns a newly allocated lstelem (new_elem) from a lstelem containing a string in its content(elem).
  318. The new lstelem applies f to every char in the content of elem while building new_elem.
  319. Behavior if elem->content is not nul-terminated is undefined.*/
  320. t_list  *ft_lstelem_strmap(t_list *elem, char (*f)(char))
  321. {
  322.     t_list  *new_elem;
  323.     char    *new_str;
  324.  
  325.     if (elem == NULL || elem->content == NULL)
  326.         return (NULL);
  327.     if (!(new_elem = (t_list*)malloc(sizeof(t_list))))
  328.         return (NULL);
  329.     if (!(new_str = ft_strmap(elem->content, f)))
  330.     {
  331.         free(new_elem);
  332.         return (NULL);
  333.     }
  334.     new_elem->content = new_str;
  335.     new_elem->content_size = elem->content_size;
  336.     return (new_elem);
  337. }
  338. void    ft_lstiter_toupper(t_list *elem)
  339. {
  340.     ft_lstelem_strmap(elem, toupper_chr);
  341. }
  342. t_list  *ft_lstmap_toupper(t_list *elem)
  343. {
  344.     return ft_lstelem_strmap(elem, toupper_chr);
  345. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement