File indexing completed on 2024-04-14 15:50:45
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 #define TEST_FORMAT "%d ... %x \"%s\" kkk %d", 1023, 999, "als8fausdfas", 40 0026 0027 using namespace QtCurve; 0028 0029 int 0030 main() 0031 { 0032 char static_res[1024]; 0033 sprintf(static_res, TEST_FORMAT); 0034 0035 char *asprintf_res; 0036 asprintf(&asprintf_res, TEST_FORMAT); 0037 0038 char *m_res1 = Str::format<false>(nullptr, nullptr, TEST_FORMAT); 0039 size_t size = 10; 0040 char *m_res2 = Str::format((char*)malloc(10), &size, TEST_FORMAT); 0041 assert(size > strlen(m_res2)); 0042 0043 size = strlen(m_res2); 0044 char *m_res3 = Str::format((char*)malloc(size), &size, TEST_FORMAT); 0045 assert(size > strlen(m_res3)); 0046 0047 size = strlen(m_res3) + 1; 0048 char *buff4 = (char*)malloc(size); 0049 char *m_res4 = Str::format(buff4, &size, TEST_FORMAT); 0050 assert(m_res4 == buff4); 0051 assert(size == strlen(m_res4) + 1); 0052 0053 char buff5[size]; 0054 char *m_res5 = Str::format<false>(buff5, &size, TEST_FORMAT); 0055 assert(m_res5 == buff5); 0056 assert(size == strlen(m_res5) + 1); 0057 0058 size--; 0059 char buff6[size]; 0060 char *m_res6 = Str::format<false>(buff6, &size, TEST_FORMAT); 0061 assert(m_res6 != buff6); 0062 assert(size > strlen(m_res6) + 1); 0063 0064 Str::Buff<10> buff7(10); 0065 assert(buff7.is_static() && buff7.size() == 10); 0066 char *m_res7 = buff7.printf(TEST_FORMAT); 0067 assert(buff7.get() == m_res7); 0068 assert(!buff7.is_static()); 0069 0070 Str::Buff<10> buff8(11); 0071 assert(!buff8.is_static() && buff8.size() == 11); 0072 char *old_p8 = buff8.get(); 0073 char *m_res8 = buff8.printf(TEST_FORMAT); 0074 assert(buff8.get() == m_res8); 0075 assert(buff8.get() != old_p8); 0076 0077 Str::Buff<10> buff9(1024); 0078 assert(!buff9.is_static() && buff9.size() == 1024); 0079 char *old_p9 = buff9.get(); 0080 char *m_res9 = buff9.printf(TEST_FORMAT); 0081 assert(buff9.get() == m_res9); 0082 assert(buff9.get() == old_p9); 0083 0084 Str::Buff<2048> buff10(1024); 0085 assert(buff10.is_static() && buff10.size() == 1024); 0086 char *old_p10 = buff10.get(); 0087 char *m_res10 = buff10.printf(TEST_FORMAT); 0088 assert(buff10.get() == m_res10); 0089 assert(buff10.get() == old_p10); 0090 assert(buff10.is_static()); 0091 0092 const char *const results[] = { 0093 static_res, 0094 asprintf_res, 0095 m_res1, 0096 m_res2, 0097 m_res3, 0098 m_res4, 0099 m_res5, 0100 m_res6, 0101 m_res7, 0102 m_res8, 0103 m_res9, 0104 m_res10, 0105 }; 0106 for (unsigned i = 0;i < sizeof(results) / sizeof(results[0]);i++) { 0107 for (unsigned j = 0;j < sizeof(results) / sizeof(results[0]);j++) { 0108 assert(strcmp(results[i], results[j]) == 0); 0109 } 0110 } 0111 free(asprintf_res); 0112 free(m_res1); 0113 free(m_res2); 0114 free(m_res3); 0115 free(m_res4); 0116 free(m_res6); 0117 return 0; 0118 }