<commit_before>#include "vm/test/test.hpp"

#include "builtin/string.hpp"

class TestString : public CxxTest::TestSuite, public VMTest {
public:

  String *str;

  void setUp() {
    create();
  }

  void tearDown() {
    destroy();
  }

  void test_create_with_size() {
    str = String::create(state, Fixnum::from(4));
    TS_ASSERT_EQUALS(str->size(), 4);
    TS_ASSERT_EQUALS(str->data()->size(), 8);
  }

  void test_create() {
    str = String::create(state, "blah");
    TS_ASSERT_EQUALS(str->size(), 4);
  }

  void test_create_with_substring() {
    str = String::create(state, "blah", 2);
    TS_ASSERT_EQUALS(str->size(), 2);
    TS_ASSERT_SAME_DATA("bl\0", str->c_str(state), 3);
  }

  void test_create_with_null_and_zero_count() {
    str = String::create(state, NULL, 0);
    TS_ASSERT_EQUALS(str->size(), 0);
    TS_ASSERT_EQUALS(str->byte_address()[0], 0);
  }

  void test_hash_string() {
    str = String::create(state, "blah");
    hashval hash = str->hash_string(state);
    TS_ASSERT(hash > 0);

    hashval again = str->hash_string(state);

    TS_ASSERT_EQUALS(hash, again);

    String* another_str = String::create(state, "blah");
    hashval another_hash = another_str->hash_string(state);

    TS_ASSERT_EQUALS(hash, another_hash);
  }

  void test_to_sym() {
    str = String::create(state, "blah");
    Object* sym = str->to_sym(state);

    TS_ASSERT(sym->symbol_p());
  }

  void test_string_dup() {
    str = String::create(state, "blah");
    String* str2 = str->string_dup(state);

    TS_ASSERT_EQUALS(str->shared(), Qtrue);
    TS_ASSERT_EQUALS(str2->shared(), Qtrue);

    TS_ASSERT_EQUALS(str->data(), str2->data());
  }

  void test_unshare() {
    str = String::create(state, "blah");
    String* str2 = str->string_dup(state);

    str->unshare(state);

    TS_ASSERT(str->data() != str2->data());
    TS_ASSERT_EQUALS(std::string("blah"), str->c_str(state));
  }

  void test_cstr() {
    // We need to setup a string that uses a CharArray
    // to full capacity in order to test c_str() resizing
    str = String::create(state, "zzzzzzz");
    str->num_bytes(state, Fixnum::from(8));
    str->byte_address()[7] = 'z';

    // We should still have a data backend of 8 bytes
    // which are completely filled by all 'z' characters
    TS_ASSERT_EQUALS(str->data()->size(), 8);
    TS_ASSERT(!memcmp("zzzzzzzz", str->byte_address(), 8));

    const char* ptr = str->c_str(state);
    // Sizes are aligned so they end up as 16 bytes used
    TS_ASSERT_EQUALS(str->data()->size(), 16);
    TS_ASSERT(!memcmp("zzzzzzzz\0", ptr, 9));
    TS_ASSERT(!memcmp("zzzzzzzz\0", str->byte_address(), 9));<commit_msg>Use an assertion that doesn't depend on platform size<commit_after>#include "vm/test/test.hpp"

#include "builtin/string.hpp"

class TestString : public CxxTest::TestSuite, public VMTest {
public:

  String *str;

  void setUp() {
    create();
  }

  void tearDown() {
    destroy();
  }

  void test_create_with_size() {
    str = String::create(state, Fixnum::from(4));
    TS_ASSERT_EQUALS(str->size(), 4);
    TS_ASSERT_EQUALS(str->data()->size(), 8);
  }

  void test_create() {
    str = String::create(state, "blah");
    TS_ASSERT_EQUALS(str->size(), 4);
  }

  void test_create_with_substring() {
    str = String::create(state, "blah", 2);
    TS_ASSERT_EQUALS(str->size(), 2);
    TS_ASSERT_SAME_DATA("bl\0", str->c_str(state), 3);
  }

  void test_create_with_null_and_zero_count() {
    str = String::create(state, NULL, 0);
    TS_ASSERT_EQUALS(str->size(), 0);
    TS_ASSERT_EQUALS(str->byte_address()[0], 0);
  }

  void test_hash_string() {
    str = String::create(state, "blah");
    hashval hash = str->hash_string(state);
    TS_ASSERT(hash > 0);

    hashval again = str->hash_string(state);

    TS_ASSERT_EQUALS(hash, again);

    String* another_str = String::create(state, "blah");
    hashval another_hash = another_str->hash_string(state);

    TS_ASSERT_EQUALS(hash, another_hash);
  }

  void test_to_sym() {
    str = String::create(state, "blah");
    Object* sym = str->to_sym(state);

    TS_ASSERT(sym->symbol_p());
  }

  void test_string_dup() {
    str = String::create(state, "blah");
    String* str2 = str->string_dup(state);

    TS_ASSERT_EQUALS(str->shared(), Qtrue);
    TS_ASSERT_EQUALS(str2->shared(), Qtrue);

    TS_ASSERT_EQUALS(str->data(), str2->data());
  }

  void test_unshare() {
    str = String::create(state, "blah");
    String* str2 = str->string_dup(state);

    str->unshare(state);

    TS_ASSERT(str->data() != str2->data());
    TS_ASSERT_EQUALS(std::string("blah"), str->c_str(state));
  }

  void test_cstr() {
    // We need to setup a string that uses a CharArray
    // to full capacity in order to test c_str() resizing
    str = String::create(state, "zzzzzzz");
    str->num_bytes(state, Fixnum::from(8));
    str->byte_address()[7] = 'z';

    // We should still have a data backend of 8 bytes
    // which are completely filled by all 'z' characters
    TS_ASSERT_EQUALS(str->data()->size(), 8);
    TS_ASSERT(!memcmp("zzzzzzzz", str->byte_address(), 8));

    const char* ptr = str->c_str(state);
    // Sizes are aligned so they end up as 16 bytes used
    TS_ASSERT(str->data()->size() > 8);
    TS_ASSERT(!memcmp("zzzzzzzz\0", ptr, 9));
    TS_ASSERT(!memcmp("zzzzzzzz\0", str->byte_address(), 9));
{'fraction_non_alphanumeric': 0.1703286384976526, 'fraction_numerical': 0.011455399061032864, 'mean_word_length': 3.815551537070524, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 70, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 8, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2064671', 'n_tokens_mistral': 2007, 'n_tokens_neox': 1908, 'n_words': 387}