File indexing completed on 2024-03-24 17:24:18

0001 /*****************************************************************************
0002  *   Copyright 2013 - 2015 Yichao Yu <yyc1992@gmail.com>                     *
0003  *                                                                           *
0004  *   This program is free software; you can redistribute it and/or modify    *
0005  *   it under the terms of the GNU Lesser General Public License as          *
0006  *   published by the Free Software Foundation; either version 2.1 of the    *
0007  *   License, or (at your option) version 3, or any later version accepted   *
0008  *   by the membership of KDE e.V. (or its successor approved by the         *
0009  *   membership of KDE e.V.), which shall act as a proxy defined in          *
0010  *   Section 6 of version 3 of the license.                                  *
0011  *                                                                           *
0012  *   This program is distributed in the hope that it will be useful,         *
0013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
0014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       *
0015  *   Lesser General Public License for more details.                         *
0016  *                                                                           *
0017  *   You should have received a copy of the GNU Lesser General Public        *
0018  *   License along with this library. If not,                                *
0019  *   see <http://www.gnu.org/licenses/>.                                     *
0020  *****************************************************************************/
0021 
0022 #include <qtcurve-utils/strs.h>
0023 #include <assert.h>
0024 
0025 using namespace QtCurve;
0026 
0027 static const char *str1 = "abcdef;;\\;;aa\\\\a;a\\bb;";
0028 static const char *str_list1[] = {"abcdef", "", ";", "aa\\a", "abb", ""};
0029 
0030 static const char *str2 = "abcdef,ls,\\";
0031 static const char *str_list2[] = {"abcdef", "ls", ""};
0032 
0033 #define _TO_STR(args...)                        \
0034     __TO_STR(args)
0035 #define __TO_STR(args...)                       \
0036     #args
0037 
0038 #define INT_LIST 1, 2, 3, 4, 5, 1, -2, -3, -5, 9, -10
0039 #define FLOAT_LIST .1, 0.32, 8.3, 4.8, 4.59, 1.9, -2.10, -3.38, -8.5, 9., -10.9
0040 
0041 static const char *int_str = _TO_STR(INT_LIST);
0042 static const long int_list[] = {INT_LIST};
0043 
0044 static const char *float_str = _TO_STR(FLOAT_LIST);
0045 static const double float_list[] = {FLOAT_LIST};
0046 
0047 typedef struct {
0048     const char **strs;
0049     int index;
0050 } QtcStrListTest;
0051 
0052 static bool
0053 qtcStrListFunc(const char *str, size_t len, QtcStrListTest *data)
0054 {
0055     assert(strlen(str) == len);
0056     assert(strcmp(str, data->strs[data->index]) == 0);
0057     data->index++;
0058     return true;
0059 }
0060 
0061 static bool
0062 qtcStrListFunc2(const char *str, QtcStrListTest *data)
0063 {
0064     assert(strcmp(str, data->strs[data->index]) == 0);
0065     data->index++;
0066     return true;
0067 }
0068 
0069 int
0070 main()
0071 {
0072     QtcStrListTest test1 = {
0073         .strs = str_list1,
0074         .index = 0,
0075     };
0076     StrList::forEach(str1, ';', qtcStrListFunc, &test1);
0077     assert(test1.index == sizeof(str_list1) / sizeof(char*));
0078     test1.index = 0;
0079     StrList::forEach(str1, ';', qtcStrListFunc2, &test1);
0080     assert(test1.index == sizeof(str_list1) / sizeof(char*));
0081     QtcStrListTest test2 = {
0082         .strs = str_list2,
0083         .index = 0,
0084     };
0085     StrList::forEach(str2, qtcStrListFunc, &test2);
0086     assert(test2.index == sizeof(str_list2) / sizeof(char*));
0087     test2.index = 0;
0088     StrList::forEach(str2, qtcStrListFunc2, &test2);
0089     assert(test2.index == sizeof(str_list2) / sizeof(char*));
0090 
0091     size_t list1_len;
0092     char **list1 = qtcStrLoadStrList(str1, ';', , &list1_len, , ,);
0093     assert(list1_len == (sizeof(str_list1) / sizeof(char*)));
0094     for (unsigned i = 0;i < list1_len;i++) {
0095         assert(strcmp(str_list1[i], list1[i]) == 0);
0096         free(list1[i]);
0097     }
0098     free(list1);
0099 
0100     size_t list2_len;
0101     char **list2 = qtcStrLoadStrList(str2, , , &list2_len, , ,);
0102     assert(list2_len == (sizeof(str_list2) / sizeof(char*)));
0103     for (unsigned i = 0;i < list2_len;i++) {
0104         assert(strcmp(str_list2[i], list2[i]) == 0);
0105         free(list2[i]);
0106     }
0107     free(list2);
0108 
0109     size_t int_list_len;
0110     long *int_list_res = qtcStrLoadIntList(int_str, ',', , &int_list_len);
0111     assert(int_list_len == (sizeof(int_list) / sizeof(long)));
0112     assert(memcmp(int_list, int_list_res, sizeof(int_list)) == 0);
0113     free(int_list_res);
0114 
0115     size_t float_list_len;
0116     double *float_list_res = qtcStrLoadFloatList(float_str, ',', ,
0117                                                  &float_list_len);
0118     assert(float_list_len == (sizeof(float_list) / sizeof(double)));
0119     assert(memcmp(float_list, float_list_res, sizeof(float_list)) == 0);
0120     free(float_list_res);
0121 
0122     size_t float_list_len2 = 3;
0123     double static_float_list[3];
0124     double *float_list_res2 =
0125         qtcStrLoadFloatList(float_str, ',', , &float_list_len2,
0126                             static_float_list,
0127                             sizeof(static_float_list) / sizeof(double));
0128     assert(float_list_len2 == (sizeof(static_float_list) / sizeof(double)));
0129     assert(memcmp(float_list, float_list_res2, sizeof(static_float_list)) == 0);
0130     return 0;
0131 }