2013-08-28 00:44:27 +00:00
|
|
|
// Unit Tests for Scintilla internal data structures
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include "Platform.h"
|
|
|
|
|
|
|
|
#include "CharClassify.h"
|
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
#include "catch.hpp"
|
2013-08-28 00:44:27 +00:00
|
|
|
|
|
|
|
// Test CharClassify.
|
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
class CharClassifyTest {
|
|
|
|
// Avoid warnings, private so never called.
|
|
|
|
CharClassifyTest(const CharClassifyTest &);
|
2013-08-28 00:44:27 +00:00
|
|
|
protected:
|
2015-06-07 21:19:26 +00:00
|
|
|
CharClassifyTest() {
|
2013-08-28 00:44:27 +00:00
|
|
|
pcc = new CharClassify();
|
|
|
|
for (int ch = 0; ch < 256; ch++) {
|
|
|
|
if (ch == '\r' || ch == '\n')
|
|
|
|
charClass[ch] = CharClassify::ccNewLine;
|
|
|
|
else if (ch < 0x20 || ch == ' ')
|
|
|
|
charClass[ch] = CharClassify::ccSpace;
|
|
|
|
else if (ch >= 0x80 || isalnum(ch) || ch == '_')
|
|
|
|
charClass[ch] = CharClassify::ccWord;
|
|
|
|
else
|
|
|
|
charClass[ch] = CharClassify::ccPunctuation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
~CharClassifyTest() {
|
2013-08-28 00:44:27 +00:00
|
|
|
delete pcc;
|
|
|
|
pcc = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
CharClassify *pcc;
|
|
|
|
CharClassify::cc charClass[256];
|
|
|
|
|
|
|
|
static const char* GetClassName(CharClassify::cc charClass) {
|
|
|
|
switch(charClass) {
|
|
|
|
#define CASE(c) case CharClassify::c: return #c
|
|
|
|
CASE(ccSpace);
|
|
|
|
CASE(ccNewLine);
|
|
|
|
CASE(ccWord);
|
|
|
|
CASE(ccPunctuation);
|
|
|
|
#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++) {
|
|
|
|
CharClassify::cc thisClass = CharClassify::cc(i % 4);
|
|
|
|
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++) {
|
|
|
|
CharClassify::cc thisClass = CharClassify::cc(i % 4);
|
|
|
|
buf[0] = i;
|
|
|
|
pcc->SetCharClasses(buf, thisClass);
|
|
|
|
charClass[i] = thisClass;
|
|
|
|
}
|
|
|
|
for (int classVal = 0; classVal < 4; ++classVal) {
|
|
|
|
CharClassify::cc thisClass = CharClassify::cc(classVal % 4);
|
|
|
|
int size = pcc->GetCharsOfClass(thisClass, NULL);
|
2015-06-07 21:19:26 +00:00
|
|
|
unsigned char* buffer = new unsigned char[size + 1];
|
2013-08-28 00:44:27 +00:00
|
|
|
buffer[size] = '\0';
|
|
|
|
pcc->GetCharsOfClass(thisClass, buffer);
|
|
|
|
for (int i = 1; i < 256; i++) {
|
|
|
|
if (charClass[i] == thisClass) {
|
2015-06-07 21:19:26 +00:00
|
|
|
if (!memchr(reinterpret_cast<char*>(buffer), i, size))
|
|
|
|
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;
|
|
|
|
REQUIRE(memchr(reinterpret_cast<char*>(buffer), i, size));
|
2013-08-28 00:44:27 +00:00
|
|
|
} else {
|
2015-06-07 21:19:26 +00:00
|
|
|
if (memchr(reinterpret_cast<char*>(buffer), i, size))
|
|
|
|
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;
|
|
|
|
REQUIRE_FALSE(memchr(reinterpret_cast<char*>(buffer), i, size));
|
2013-08-28 00:44:27 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-07 21:19:26 +00:00
|
|
|
delete []buffer;
|
2013-08-28 00:44:27 +00:00
|
|
|
}
|
|
|
|
}
|