New upstream version 18.02
[deb_dpdk.git] / test / test / test_string_fns.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <stdarg.h>
7 #include <stddef.h>
8 #include <errno.h>
9 #include <string.h>
10
11 #include <rte_string_fns.h>
12
13 #include "test.h"
14
15 #define LOG(...) do {\
16         fprintf(stderr, "%s() ln %d: ", __func__, __LINE__); \
17         fprintf(stderr, __VA_ARGS__); \
18 } while(0)
19
20 #define DATA_BYTE 'a'
21
22 static int
23 test_rte_strsplit(void)
24 {
25         int i;
26         do {
27                 /* =======================================================
28                  * split a mac address correct number of splits requested
29                  * =======================================================*/
30                 char test_string[] = "54:65:76:87:98:90";
31                 char *splits[6];
32
33                 LOG("Source string: '%s', to split on ':'\n", test_string);
34                 if (rte_strsplit(test_string, sizeof(test_string),
35                                 splits, 6, ':') != 6) {
36                         LOG("Error splitting mac address\n");
37                         return -1;
38                 }
39                 for (i = 0; i < 6; i++)
40                         LOG("Token %d = %s\n", i + 1, splits[i]);
41         } while (0);
42
43
44         do {
45                 /* =======================================================
46                  * split on spaces smaller number of splits requested
47                  * =======================================================*/
48                 char test_string[] = "54 65 76 87 98 90";
49                 char *splits[6];
50
51                 LOG("Source string: '%s', to split on ' '\n", test_string);
52                 if (rte_strsplit(test_string, sizeof(test_string),
53                                 splits, 3, ' ') != 3) {
54                         LOG("Error splitting mac address for max 2 splits\n");
55                         return -1;
56                 }
57                 for (i = 0; i < 3; i++)
58                         LOG("Token %d = %s\n", i + 1, splits[i]);
59         } while (0);
60
61         do {
62                 /* =======================================================
63                  * split on commas - more splits than commas requested
64                  * =======================================================*/
65                 char test_string[] = "a,b,c,d";
66                 char *splits[6];
67
68                 LOG("Source string: '%s', to split on ','\n", test_string);
69                 if (rte_strsplit(test_string, sizeof(test_string),
70                                 splits, 6, ',') != 4) {
71                         LOG("Error splitting %s on ','\n", test_string);
72                         return -1;
73                 }
74                 for (i = 0; i < 4; i++)
75                         LOG("Token %d = %s\n", i + 1, splits[i]);
76         } while(0);
77
78         do {
79                 /* =======================================================
80                  * Try splitting on non-existent character.
81                  * =======================================================*/
82                 char test_string[] = "a,b,c,d";
83                 char *splits[6];
84
85                 LOG("Source string: '%s', to split on ' '\n", test_string);
86                 if (rte_strsplit(test_string, sizeof(test_string),
87                                 splits, 6, ' ') != 1) {
88                         LOG("Error splitting %s on ' '\n", test_string);
89                         return -1;
90                 }
91                 LOG("String not split\n");
92         } while(0);
93
94         do {
95                 /* =======================================================
96                  * Invalid / edge case parameter checks
97                  * =======================================================*/
98                 char test_string[] = "a,b,c,d";
99                 char *splits[6];
100
101                 if (rte_strsplit(NULL, 0, splits, 6, ',') >= 0
102                                 || errno != EINVAL){
103                         LOG("Error: rte_strsplit accepted NULL string parameter\n");
104                         return -1;
105                 }
106
107                 if (rte_strsplit(test_string, sizeof(test_string), NULL, 0, ',') >= 0
108                                 || errno != EINVAL){
109                         LOG("Error: rte_strsplit accepted NULL array parameter\n");
110                         return -1;
111                 }
112
113                 errno = 0;
114                 if (rte_strsplit(test_string, 0, splits, 6, ',') != 0 || errno != 0) {
115                         LOG("Error: rte_strsplit did not accept 0 length string\n");
116                         return -1;
117                 }
118
119                 if (rte_strsplit(test_string, sizeof(test_string), splits, 0, ',') != 0
120                                 || errno != 0) {
121                         LOG("Error: rte_strsplit did not accept 0 length array\n");
122                         return -1;
123                 }
124
125                 LOG("Parameter test cases passed\n");
126         } while(0);
127
128         LOG("%s - PASSED\n", __func__);
129         return 0;
130 }
131
132 static int
133 test_string_fns(void)
134 {
135         if (test_rte_strsplit() < 0)
136                 return -1;
137         return 0;
138 }
139
140 REGISTER_TEST_COMMAND(string_autotest, test_string_fns);