File indexing completed on 2024-04-21 05:18:08
0001 /* 0002 SPDX-FileCopyrightText: 2009 Constantin Berzan <exit3219@gmail.com> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "charfreqtest.h" 0008 #include <QTest> 0009 0010 0011 #include <../src/kmime_charfreq.cpp> 0012 using namespace KMime; 0013 0014 QTEST_MAIN(CharFreqTest) 0015 0016 void CharFreqTest::test8bitData() 0017 { 0018 { 0019 // If it has NUL then it's Binary (equivalent to EightBitData in CharFreq). 0020 QByteArray data("123"); 0021 data += char(0); 0022 data += "test"; 0023 CharFreq cf(data); 0024 QCOMPARE(cf.type(), CharFreq::Binary); 0025 } 0026 0027 { 0028 // If it has lines longer than 998, it's EightBitData. 0029 QByteArray data; 0030 for (int i = 0; i < 999; i++) { 0031 data += char(169); 0032 } 0033 CharFreq cf(data); 0034 QCOMPARE(cf.type(), CharFreq::EightBitData); 0035 } 0036 0037 { 0038 // If #CR != #CRLF then it's EightBitData. 0039 QByteArray data("©line1\r\nline2\r"); 0040 CharFreq cf(data); 0041 QCOMPARE(cf.type(), CharFreq::EightBitData); 0042 } 0043 0044 { 0045 // If #LF != #CRLF then it's EightBitData. 0046 QByteArray data("©line1\r\nline2\n"); 0047 CharFreq cf(data); 0048 QCOMPARE(cf.type(), CharFreq::EightBitData); 0049 } 0050 0051 { 0052 // If it has a lot of control chars, it's EightBitData. 0053 QByteArray data("©test\a\a\a\a\a\a\a"); 0054 CharFreq cf(data); 0055 QCOMPARE(cf.type(), CharFreq::EightBitData); 0056 } 0057 } 0058 0059 void CharFreqTest::test8bitText() 0060 { 0061 { 0062 // If the text only contains newlines and some random accented chars, then it is EightBitText 0063 QByteArray data("asdfasdfasdfasdfasdfasdfäöü\n"); 0064 CharFreq cf(data); 0065 QCOMPARE(cf.type(), CharFreq::EightBitText); 0066 } 0067 0068 { 0069 // If it has no NULs, few CTLs, and only CRLFs, it's EightBitText. 0070 QByteArray data("©beware the beast but enjoy the feast he offers...\r\n"); 0071 CharFreq cf(data); 0072 QCOMPARE(cf.type(), CharFreq::EightBitText); 0073 } 0074 } 0075 0076 void CharFreqTest::test7bitData() 0077 { 0078 { 0079 // If it has lines longer than 998, it's SevenBitData. 0080 QByteArray data; 0081 for (int i = 0; i < 999; i++) { 0082 data += 'a'; 0083 } 0084 CharFreq cf(data); 0085 QCOMPARE(cf.type(), CharFreq::SevenBitData); 0086 } 0087 0088 { 0089 // If #CR != #CRLF then it's SevenBitData. 0090 QByteArray data("line1\r\nline2\r"); 0091 CharFreq cf(data); 0092 QCOMPARE(cf.type(), CharFreq::SevenBitData); 0093 } 0094 0095 { 0096 // If #LF != #CRLF then it's SevenBitData. 0097 QByteArray data("line1\r\nline2\n"); 0098 CharFreq cf(data); 0099 QCOMPARE(cf.type(), CharFreq::SevenBitData); 0100 } 0101 0102 { 0103 // If it has a lot of control chars, it's SevenBitData. 0104 QByteArray data("test\a\a\a\a\a\a\a"); 0105 CharFreq cf(data); 0106 QCOMPARE(cf.type(), CharFreq::SevenBitData); 0107 } 0108 } 0109 0110 void CharFreqTest::test7bitText() 0111 { 0112 { 0113 // If the text only contains newlines, then it is SevenBitText 0114 QByteArray data("line1\nline2\n"); 0115 CharFreq cf(data); 0116 QCOMPARE(cf.type(), CharFreq::SevenBitText); 0117 } 0118 0119 { 0120 // If it has no NULs, few CTLs, and only CRLFs, it's SevenBitText. 0121 QByteArray data("beware the beast but enjoy the feast he offers...\r\n"); 0122 CharFreq cf(data); 0123 QCOMPARE(cf.type(), CharFreq::SevenBitText); 0124 } 0125 } 0126 0127 void CharFreqTest::testTrailingWhitespace() 0128 { 0129 QByteArray data("test "); 0130 CharFreq cf(data); 0131 QVERIFY(cf.hasTrailingWhitespace()); 0132 } 0133 0134 void CharFreqTest::testLeadingFrom() 0135 { 0136 QByteArray data("From here thither"); 0137 CharFreq cf(data); 0138 QVERIFY(cf.hasLeadingFrom()); 0139 } 0140 0141 0142 #include "moc_charfreqtest.cpp"