2022-01-04 23:07:50 +00:00
|
|
|
/** @file testCharClassify.cxx
|
|
|
|
** Unit Tests for Scintilla internal data structures
|
|
|
|
**/
|
2013-08-28 00:44:27 +00:00
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
#include <cstring>
|
2013-08-28 00:44:27 +00:00
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
#include <string_view>
|
|
|
|
#include <vector>
|
2022-01-04 23:07:50 +00:00
|
|
|
#include <optional>
|
2013-08-28 00:44:27 +00:00
|
|
|
#include <algorithm>
|
2019-05-04 18:14:48 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <iostream>
|
2013-08-28 00:44:27 +00:00
|
|
|
|
2022-01-04 23:07:50 +00:00
|
|
|
#include "Debugging.h"
|
2013-08-28 00:44:27 +00:00
|
|
|
|
|
|
|
#include "CharClassify.h"
|
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
#include "catch.hpp"
|
2013-08-28 00:44:27 +00:00
|
|
|
|
2022-01-04 23:07:50 +00:00
|
|
|
using namespace Scintilla::Internal;
|
2019-05-04 18:14:48 +00:00
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
// Test CharClassify.
|
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
class CharClassifyTest {
|
2019-05-04 18:14:48 +00:00
|
|
|
// Avoid warnings, deleted so never called.
|
|
|
|
CharClassifyTest(const CharClassifyTest &) = delete;
|
2013-08-28 00:44:27 +00:00
|
|
|
protected:
|
2015-06-07 21:19:26 +00:00
|
|
|
CharClassifyTest() {
|
2022-01-04 23:07:50 +00:00
|
|
|
pcc = std::make_unique<CharClassify>();
|
2013-08-28 00:44:27 +00:00
|
|
|
for (int ch = 0; ch < 256; ch++) {
|
|
|
|
if (ch == '\r' || ch == '\n')
|
2022-01-04 23:07:50 +00:00
|
|
|
charClass[ch] = CharacterClass::newLine;
|
|
|
|
else if (ch < 0x20 || ch == ' ' || ch == '\x7f')
|
|
|
|
charClass[ch] = CharacterClass::space;
|
2013-08-28 00:44:27 +00:00
|
|
|
else if (ch >= 0x80 || isalnum(ch) || ch == '_')
|
2022-01-04 23:07:50 +00:00
|
|
|
charClass[ch] = CharacterClass::word;
|
2013-08-28 00:44:27 +00:00
|
|
|
else
|
2022-01-04 23:07:50 +00:00
|
|
|
charClass[ch] = CharacterClass::punctuation;
|
2013-08-28 00:44:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
std::unique_ptr<CharClassify> pcc;
|
2022-01-04 23:07:50 +00:00
|
|
|
CharacterClass charClass[256];
|
2013-08-28 00:44:27 +00:00
|
|
|
|
2022-01-04 23:07:50 +00:00
|
|
|
static const char* GetClassName(CharacterClass charClass) {
|
2013-08-28 00:44:27 +00:00
|
|
|
switch(charClass) {
|
2022-01-04 23:07:50 +00:00
|
|
|
#define CASE(c) case CharacterClass::c: return #c
|
|
|
|
CASE(space);
|
|
|
|
CASE(newLine);
|
|
|
|
CASE(word);
|
|
|
|
CASE(punctuation);
|
2013-08-28 00:44:27 +00:00
|
|
|
#undef CASE
|
|
|
|
default:
|
|
|
|
return "<unknown>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
TEST_CASE_METHOD(CharClassifyTest, "Defaults") {
|
2013-08-28 00:44:27 +00:00
|
|
|
for (int i = 0; i < 256; i++) {
|
2015-06-07 21:19:26 +00:00
|
|
|
if (charClass[i] != pcc->GetClass(i))
|
|
|
|
std::cerr
|
2013-08-28 00:44:27 +00:00
|
|
|
<< "Character " << i
|
|
|
|
<< " should be class " << GetClassName(charClass[i])
|
2015-06-07 21:19:26 +00:00
|
|
|
<< ", but got " << GetClassName(pcc->GetClass(i)) << std::endl;
|
|
|
|
REQUIRE(charClass[i] == pcc->GetClass(i));
|
2013-08-28 00:44:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
TEST_CASE_METHOD(CharClassifyTest, "Custom") {
|
2013-08-28 00:44:27 +00:00
|
|
|
unsigned char buf[2] = {0, 0};
|
|
|
|
for (int i = 0; i < 256; i++) {
|
2022-01-04 23:07:50 +00:00
|
|
|
CharacterClass thisClass = CharacterClass(i % 4);
|
2013-08-28 00:44:27 +00:00
|
|
|
buf[0] = i;
|
|
|
|
pcc->SetCharClasses(buf, thisClass);
|
|
|
|
charClass[i] = thisClass;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < 256; i++) {
|
2015-06-07 21:19:26 +00:00
|
|
|
if (charClass[i] != pcc->GetClass(i))
|
|
|
|
std::cerr
|
2013-08-28 00:44:27 +00:00
|
|
|
<< "Character " << i
|
|
|
|
<< " should be class " << GetClassName(charClass[i])
|
2015-06-07 21:19:26 +00:00
|
|
|
<< ", but got " << GetClassName(pcc->GetClass(i)) << std::endl;
|
|
|
|
REQUIRE(charClass[i] == pcc->GetClass(i));
|
2013-08-28 00:44:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
TEST_CASE_METHOD(CharClassifyTest, "CharsOfClass") {
|
2013-08-28 00:44:27 +00:00
|
|
|
unsigned char buf[2] = {0, 0};
|
|
|
|
for (int i = 1; i < 256; i++) {
|
2022-01-04 23:07:50 +00:00
|
|
|
CharacterClass thisClass = CharacterClass(i % 4);
|
2013-08-28 00:44:27 +00:00
|
|
|
buf[0] = i;
|
|
|
|
pcc->SetCharClasses(buf, thisClass);
|
|
|
|
charClass[i] = thisClass;
|
|
|
|
}
|
|
|
|
for (int classVal = 0; classVal < 4; ++classVal) {
|
2022-01-04 23:07:50 +00:00
|
|
|
CharacterClass thisClass = CharacterClass(classVal % 4);
|
2013-08-28 00:44:27 +00:00
|
|
|
int size = pcc->GetCharsOfClass(thisClass, NULL);
|
2019-05-04 18:14:48 +00:00
|
|
|
std::vector<unsigned char> buffer(size+1);
|
2022-01-04 23:07:50 +00:00
|
|
|
void *pBuffer = static_cast<void *>(buffer.data());
|
|
|
|
pcc->GetCharsOfClass(thisClass, buffer.data());
|
2013-08-28 00:44:27 +00:00
|
|
|
for (int i = 1; i < 256; i++) {
|
|
|
|
if (charClass[i] == thisClass) {
|
2022-01-04 23:07:50 +00:00
|
|
|
if (!memchr(pBuffer, i, size))
|
2015-06-07 21:19:26 +00:00
|
|
|
std::cerr
|
2013-08-28 00:44:27 +00:00
|
|
|
<< "Character " << i
|
|
|
|
<< " should be class " << GetClassName(thisClass)
|
|
|
|
<< ", but was not in GetCharsOfClass;"
|
|
|
|
<< " it is reported to be "
|
2015-06-07 21:19:26 +00:00
|
|
|
<< GetClassName(pcc->GetClass(i)) << std::endl;
|
2022-01-04 23:07:50 +00:00
|
|
|
REQUIRE(memchr(pBuffer, i, size));
|
2013-08-28 00:44:27 +00:00
|
|
|
} else {
|
2022-01-04 23:07:50 +00:00
|
|
|
if (memchr(pBuffer, i, size))
|
2015-06-07 21:19:26 +00:00
|
|
|
std::cerr
|
2013-08-28 00:44:27 +00:00
|
|
|
<< "Character " << i
|
|
|
|
<< " should not be class " << GetClassName(thisClass)
|
|
|
|
<< ", but was in GetCharsOfClass"
|
|
|
|
<< " it is reported to be "
|
2015-06-07 21:19:26 +00:00
|
|
|
<< GetClassName(pcc->GetClass(i)) << std::endl;
|
2022-01-04 23:07:50 +00:00
|
|
|
REQUIRE_FALSE(memchr(pBuffer, i, size));
|
2013-08-28 00:44:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|