Skip to content

Fix Cpp tests #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions benchmark/native/marker-index-benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

using namespace std::chrono;
using std::vector;
using uint = unsigned int;

Range get_random_range() {
Point start(rand() % 100, rand() % 100);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"build:node": "node-gyp rebuild",
"build:browser": "bash -c script/build-browser-version.sh || echo 'the browser build is unsupported'",
"build": "npm run build:node && npm run build:browser",
"test:native": "node ./script/test-native.js",
"test:native": "node-gyp build --debug --tests && node script/test-native.js",
"test:node": "mocha test/js/*.js",
"test:browser": "cross-env SUPERSTRING_USE_BROWSER_VERSION=1 mocha test/js/*.js || echo 'the browser tests are unsupported'",
"test": "npm run test:node && npm run test:native && npm run test:browser",
Expand Down
5 changes: 2 additions & 3 deletions script/test-native.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,5 @@ switch (args[0]) {
}

function run(command, args = [], options = {stdio: 'inherit'}) {
const {status} = spawnSync(command, args, options)
if (status !== 0) process.exit(status)
}
spawnSync(command, args, options)
}
20 changes: 10 additions & 10 deletions test/native/encoding-conversion-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using std::u16string;

TEST_CASE("EncodingConversion::decode - basic UTF-8") {
auto conversion = transcoding_from("UTF-8");
string input("abγdefg\nhijklmnop");
string input(u8"abγdefg\nhijklmnop");

u16string string;
conversion->decode(string, input.data(), input.size());
Expand Down Expand Up @@ -42,7 +42,7 @@ TEST_CASE("EncodingConversion::decode - invalid byte sequences in the middle of

u16string string;
conversion->decode(string, input.data(), input.size());
REQUIRE(string == u"ab" "\ufffd" "\ufffd" "de");
REQUIRE(string == u"ab" u"\ufffd" u"\ufffd" u"de");
}

TEST_CASE("EncodingConversion::decode - invalid byte sequences at the end of the input") {
Expand All @@ -58,7 +58,7 @@ TEST_CASE("EncodingConversion::decode - invalid byte sequences at the end of the
string.clear();
bytes_encoded = conversion->decode(string, input.data(), input.size(), true);
REQUIRE(bytes_encoded == 4);
REQUIRE(string == u"ab" "\ufffd" "\ufffd");
REQUIRE(string == u"ab" u"\ufffd" u"\ufffd");
}

TEST_CASE("EncodingConversion::decode - four-byte UTF-16 characters") {
Expand All @@ -67,7 +67,7 @@ TEST_CASE("EncodingConversion::decode - four-byte UTF-16 characters") {

u16string string;
conversion->decode(string, input.data(), input.size());
REQUIRE(string == u"ab" "\xd83d" "\xde01" "cd");
REQUIRE(string == u"ab" u"\xd83d" u"\xde01" u"cd");
}

TEST_CASE("EncodingConversion::encode - basic") {
Expand All @@ -93,7 +93,7 @@ TEST_CASE("EncodingConversion::encode - basic") {

TEST_CASE("EncodingConversion::encode - four-byte UTF-16 characters") {
auto conversion = transcoding_to("UTF-8");
u16string string = u"ab" "\xd83d" "\xde01" "cd"; // 'ab😁cd'
u16string string = u"ab" u"\xd83d" u"\xde01" u"cd"; // 'ab😁cd'

vector<char> output(10);
size_t bytes_encoded = 0, start = 0;
Expand All @@ -116,32 +116,32 @@ TEST_CASE("EncodingConversion::encode - four-byte UTF-16 characters") {

TEST_CASE("EncodingConversion::encode - invalid characters in the middle of the string") {
auto conversion = transcoding_to("UTF-8");
u16string string = u"abc" "\xD800" "def";
u16string string = u"abc" u"\xD800" u"def";

vector<char> output(10);
size_t bytes_encoded = 0, start = 0;

bytes_encoded = conversion->encode(
string, &start, string.size(), output.data(), output.size());
REQUIRE(std::string(output.data(), bytes_encoded) == "abc" "\ufffd" "def");
REQUIRE(std::string(output.data(), bytes_encoded) == u8"abc" u8"\ufffd" u8"def");

// Here, the invalid character occurs at the end of a chunk.
start = 0;
bytes_encoded = conversion->encode(
string, &start, 4, output.data(), output.size());
bytes_encoded += conversion->encode(
string, &start, string.size(), output.data() + bytes_encoded, output.size() - bytes_encoded);
REQUIRE(std::string(output.data(), bytes_encoded) == "abc" "\ufffd" "def");
REQUIRE(std::string(output.data(), bytes_encoded) == u8"abc" u8"\ufffd" u8"def");
}

TEST_CASE("EncodingConversion::encode - invalid characters at the end of the string") {
auto conversion = transcoding_to("UTF-8");
u16string string = u"abc" "\xD800";
u16string string = u"abc" u"\xD800";

vector<char> output(10);
size_t bytes_encoded = 0, start = 0;

bytes_encoded = conversion->encode(
string, &start, string.size(), output.data(), output.size(), true);
REQUIRE(std::string(output.data(), bytes_encoded) == "abc" "\ufffd");
REQUIRE(std::string(output.data(), bytes_encoded) == u8"abc" u8"\ufffd");
}
1 change: 1 addition & 0 deletions test/native/test-helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

using std::cout;
using std::cerr;
using uint = unsigned int;

class TextBuffer;

Expand Down
7 changes: 4 additions & 3 deletions test/native/text-buffer-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
#include "text-slice.h"
#include "regex.h"
#include <future>
#include <unistd.h>
#include <chrono>
#include <thread>

using std::move;
using std::pair;
Expand Down Expand Up @@ -466,7 +467,7 @@ TEST_CASE("TextBuffer::find_words_with_subsequence_in_range") {
}

TEST_CASE("TextBuffer::has_astral") {
REQUIRE(TextBuffer{u"ab" "\xd83d" "\xde01" "cd"}.has_astral());
REQUIRE(TextBuffer{u"ab" u"\xd83d" u"\xde01" u"cd"}.has_astral());
REQUIRE(!TextBuffer{u"abcd"}.has_astral());
}

Expand Down Expand Up @@ -535,7 +536,7 @@ TEST_CASE("TextBuffer - random edits and queries") {
Generator rand(seed);
vector<SnapshotData> results;
for (uint32_t k = 0; k < 5; k++) {
usleep(rand() % 1000);
std::this_thread::sleep_for(std::chrono::microseconds(rand() % 1000));
vector<Point> line_ending_positions;
for (uint32_t row = 0; row < snapshot->extent().row; row++) {
line_ending_positions.push_back({row, snapshot->line_length_for_row(row)});
Expand Down
Loading