⛏️ index : haiku.git

#include <stdio.h>
#include <stdlib.h>
#include <SupportDefs.h>
#include <String.h>
#include <InterfaceDefs.h>


inline void
expect(BString &string, const char *expect, size_t bytes, int32 chars)
{
	printf("expect: \"%s\" %lu %ld\n", expect, bytes, chars);
	printf("got:   \"%s\" %lu %ld\n", string.String(), string.Length(), string.CountChars());
	if (bytes != (size_t)string.Length()) {
		printf("expected byte length mismatch\n");
		exit(1);
	}

	if (chars != string.CountChars()) {
		printf("expected char count mismatch\n");
		exit(2);
	}

	if (memcmp(string.String(), expect, bytes) != 0) {
		printf("expected string mismatch\n");
		exit(3);
	}
}


int
main(int argc, char *argv[])
{
	printf("setting string to ΓΌ-Γ€-ΓΆ\n");
	BString string("ΓΌ-Γ€-ΓΆ");
	expect(string, "ΓΌ-Γ€-ΓΆ", 8, 5);

	printf("replacing ΓΌ and ΓΆ by ellipsis\n");
	string.ReplaceCharsSet("ΓΌΓΆ", B_UTF8_ELLIPSIS);
	expect(string, B_UTF8_ELLIPSIS "-Γ€-" B_UTF8_ELLIPSIS, 10, 5);

	printf("moving the last char (ellipsis) to a seperate string\n");
	BString ellipsis;
	string.MoveCharsInto(ellipsis, 4, 1);
	expect(string, B_UTF8_ELLIPSIS "-Γ€-", 7, 4);
	expect(ellipsis, B_UTF8_ELLIPSIS, 3, 1);

	printf("removing all - and ellipsis chars\n");
	string.RemoveCharsSet("-" B_UTF8_ELLIPSIS);
	expect(string, "Γ€", 2, 1);

	printf("reset the string to âÀü" B_UTF8_ELLIPSIS "âÀü\n");
	string.SetToChars("âÀü" B_UTF8_ELLIPSIS "âÀü", 5);
	expect(string, "âÀü" B_UTF8_ELLIPSIS "â", 11, 5);

	printf("truncating string to 4 characters\n");
	string.TruncateChars(4);
	expect(string, "âÀü" B_UTF8_ELLIPSIS, 9, 4);

	printf("appending 2 chars out of \"âÀü\"\n");
	string.AppendChars("âÀü", 2);
	expect(string, "âÀü" B_UTF8_ELLIPSIS "âÀ", 13, 6);

	printf("removing chars 1 through 4\n");
	string.RemoveChars(1, 3);
	expect(string, "ΓΆΓΆΓ€", 6, 3);

	printf("inserting 2 ellipsis out of 6 chars at offset 1\n");
	string.InsertChars("âÀü" B_UTF8_ELLIPSIS B_UTF8_ELLIPSIS "À", 3, 2, 1);
	expect(string, "ΓΆ" B_UTF8_ELLIPSIS B_UTF8_ELLIPSIS "ΓΆΓ€", 12, 5);

	printf("prepending 3 out of 5 chars\n");
	string.PrependChars("ÀÀ+üü", 3);
	expect(string, "ÀÀ+â" B_UTF8_ELLIPSIS B_UTF8_ELLIPSIS "âÀ", 17, 8);

	printf("comparing first 5 chars which should succeed\n");
	const char *compare = "ÀÀ+â" B_UTF8_ELLIPSIS "different";
	if (string.CompareChars(compare, 5) != 0) {
		printf("comparison failed\n");
		return 1;
	}

	printf("comparing first 6 chars which should fail\n");
	if (string.CompareChars(compare, 6) == 0) {
		printf("comparison succeeded\n");
		return 2;
	}

	printf("counting bytes of 3 chars from offset 2 expect 6\n");
	if (string.CountBytes(2, 3) != 6) {
		printf("got wrong byte count\n");
		return 3;
	}

	printf("all tests succeeded\n");
	return 0;
}