2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2013-12-12 21:11:12 +00:00
|
|
|
|
2011-07-08 17:49:22 +00:00
|
|
|
#include <stdarg.h>
|
2013-10-11 18:50:45 +00:00
|
|
|
#include <stdio.h>
|
2014-01-24 20:56:26 +00:00
|
|
|
#include "SkString.h"
|
|
|
|
#include "Test.h"
|
2011-07-08 17:49:22 +00:00
|
|
|
|
|
|
|
// Windows vsnprintf doesn't 0-terminate safely), but is so far
|
|
|
|
// encapsulated in SkString that we can't test it directly.
|
|
|
|
|
|
|
|
#ifdef SK_BUILD_FOR_WIN
|
|
|
|
#define VSNPRINTF(buffer, size, format, args) \
|
|
|
|
vsnprintf_s(buffer, size, _TRUNCATE, format, args)
|
|
|
|
#else
|
|
|
|
#define VSNPRINTF vsnprintf
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define ARGS_TO_BUFFER(format, buffer, size) \
|
|
|
|
do { \
|
|
|
|
va_list args; \
|
|
|
|
va_start(args, format); \
|
|
|
|
VSNPRINTF(buffer, size, format, args); \
|
|
|
|
va_end(args); \
|
|
|
|
} while (0)
|
|
|
|
|
2012-06-06 12:03:39 +00:00
|
|
|
static void printfAnalog(char* buffer, int size, const char format[], ...) {
|
2011-07-08 17:49:22 +00:00
|
|
|
ARGS_TO_BUFFER(format, buffer, size);
|
|
|
|
}
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(String, reporter) {
|
2009-02-27 22:06:06 +00:00
|
|
|
SkString a;
|
|
|
|
SkString b((size_t)0);
|
|
|
|
SkString c("");
|
2015-08-27 14:41:13 +00:00
|
|
|
SkString d(nullptr, 0);
|
2009-02-27 22:06:06 +00:00
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, a.isEmpty());
|
|
|
|
REPORTER_ASSERT(reporter, a == b && a == c && a == d);
|
|
|
|
|
|
|
|
a.set("hello");
|
|
|
|
b.set("hellox", 5);
|
|
|
|
c.set(a);
|
|
|
|
d.resize(5);
|
|
|
|
memcpy(d.writable_str(), "helloz", 5);
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, !a.isEmpty());
|
|
|
|
REPORTER_ASSERT(reporter, a.size() == 5);
|
|
|
|
REPORTER_ASSERT(reporter, a == b && a == c && a == d);
|
|
|
|
REPORTER_ASSERT(reporter, a.equals("hello", 5));
|
|
|
|
REPORTER_ASSERT(reporter, a.equals("hello"));
|
|
|
|
REPORTER_ASSERT(reporter, !a.equals("help"));
|
|
|
|
|
2012-04-27 17:11:31 +00:00
|
|
|
REPORTER_ASSERT(reporter, a.startsWith("hell"));
|
2012-10-29 16:42:11 +00:00
|
|
|
REPORTER_ASSERT(reporter, a.startsWith('h'));
|
2012-04-27 17:11:31 +00:00
|
|
|
REPORTER_ASSERT(reporter, !a.startsWith( "ell"));
|
2012-10-29 16:42:11 +00:00
|
|
|
REPORTER_ASSERT(reporter, !a.startsWith( 'e'));
|
2012-04-27 17:11:31 +00:00
|
|
|
REPORTER_ASSERT(reporter, a.startsWith(""));
|
|
|
|
REPORTER_ASSERT(reporter, a.endsWith("llo"));
|
2012-10-29 16:42:11 +00:00
|
|
|
REPORTER_ASSERT(reporter, a.endsWith('o'));
|
2012-04-27 17:11:31 +00:00
|
|
|
REPORTER_ASSERT(reporter, !a.endsWith("ll" ));
|
2012-10-29 16:42:11 +00:00
|
|
|
REPORTER_ASSERT(reporter, !a.endsWith('l'));
|
2012-04-27 17:11:31 +00:00
|
|
|
REPORTER_ASSERT(reporter, a.endsWith(""));
|
|
|
|
REPORTER_ASSERT(reporter, a.contains("he"));
|
|
|
|
REPORTER_ASSERT(reporter, a.contains("ll"));
|
|
|
|
REPORTER_ASSERT(reporter, a.contains("lo"));
|
|
|
|
REPORTER_ASSERT(reporter, a.contains("hello"));
|
|
|
|
REPORTER_ASSERT(reporter, !a.contains("hellohello"));
|
|
|
|
REPORTER_ASSERT(reporter, a.contains(""));
|
2012-10-29 16:42:11 +00:00
|
|
|
REPORTER_ASSERT(reporter, a.contains('e'));
|
|
|
|
REPORTER_ASSERT(reporter, !a.contains('z'));
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2009-02-27 22:06:06 +00:00
|
|
|
SkString e(a);
|
|
|
|
SkString f("hello");
|
|
|
|
SkString g("helloz", 5);
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, a == e && a == f && a == g);
|
|
|
|
|
|
|
|
b.set("world");
|
|
|
|
c = b;
|
|
|
|
REPORTER_ASSERT(reporter, a != b && a != c && b == c);
|
|
|
|
|
|
|
|
a.append(" world");
|
|
|
|
e.append("worldz", 5);
|
|
|
|
e.insert(5, " ");
|
|
|
|
f.set("world");
|
|
|
|
f.prepend("hello ");
|
|
|
|
REPORTER_ASSERT(reporter, a.equals("hello world") && a == e && a == f);
|
|
|
|
|
|
|
|
a.reset();
|
|
|
|
b.resize(0);
|
|
|
|
REPORTER_ASSERT(reporter, a.isEmpty() && b.isEmpty() && a == b);
|
|
|
|
|
|
|
|
a.set("a");
|
|
|
|
a.set("ab");
|
|
|
|
a.set("abc");
|
|
|
|
a.set("abcd");
|
2010-10-12 23:08:13 +00:00
|
|
|
|
|
|
|
a.set("");
|
2013-06-19 18:27:20 +00:00
|
|
|
a.appendS32(0x7FFFFFFFL);
|
|
|
|
REPORTER_ASSERT(reporter, a.equals("2147483647"));
|
|
|
|
a.set("");
|
|
|
|
a.appendS32(0x80000001L);
|
|
|
|
REPORTER_ASSERT(reporter, a.equals("-2147483647"));
|
|
|
|
a.set("");
|
|
|
|
a.appendS32(0x80000000L);
|
|
|
|
REPORTER_ASSERT(reporter, a.equals("-2147483648"));
|
2010-10-12 23:08:13 +00:00
|
|
|
|
|
|
|
a.set("");
|
2013-06-19 18:27:20 +00:00
|
|
|
a.appendU32(0x7FFFFFFFUL);
|
|
|
|
REPORTER_ASSERT(reporter, a.equals("2147483647"));
|
|
|
|
a.set("");
|
|
|
|
a.appendU32(0x80000001UL);
|
|
|
|
REPORTER_ASSERT(reporter, a.equals("2147483649"));
|
|
|
|
a.set("");
|
|
|
|
a.appendU32(0xFFFFFFFFUL);
|
|
|
|
REPORTER_ASSERT(reporter, a.equals("4294967295"));
|
2010-10-12 23:08:13 +00:00
|
|
|
|
|
|
|
a.set("");
|
2013-06-19 18:27:20 +00:00
|
|
|
a.appendS64(0x7FFFFFFFFFFFFFFFLL, 0);
|
|
|
|
REPORTER_ASSERT(reporter, a.equals("9223372036854775807"));
|
|
|
|
a.set("");
|
|
|
|
a.appendS64(0x8000000000000001LL, 0);
|
|
|
|
REPORTER_ASSERT(reporter, a.equals("-9223372036854775807"));
|
|
|
|
a.set("");
|
|
|
|
a.appendS64(0x8000000000000000LL, 0);
|
|
|
|
REPORTER_ASSERT(reporter, a.equals("-9223372036854775808"));
|
|
|
|
a.set("");
|
|
|
|
a.appendS64(0x0000000001000000LL, 15);
|
|
|
|
REPORTER_ASSERT(reporter, a.equals("000000016777216"));
|
|
|
|
a.set("");
|
|
|
|
a.appendS64(0xFFFFFFFFFF000000LL, 15);
|
|
|
|
REPORTER_ASSERT(reporter, a.equals("-000000016777216"));
|
2010-10-12 23:08:13 +00:00
|
|
|
|
|
|
|
a.set("");
|
2013-06-19 18:27:20 +00:00
|
|
|
a.appendU64(0x7FFFFFFFFFFFFFFFULL, 0);
|
|
|
|
REPORTER_ASSERT(reporter, a.equals("9223372036854775807"));
|
|
|
|
a.set("");
|
|
|
|
a.appendU64(0x8000000000000001ULL, 0);
|
|
|
|
REPORTER_ASSERT(reporter, a.equals("9223372036854775809"));
|
|
|
|
a.set("");
|
|
|
|
a.appendU64(0xFFFFFFFFFFFFFFFFULL, 0);
|
|
|
|
REPORTER_ASSERT(reporter, a.equals("18446744073709551615"));
|
|
|
|
a.set("");
|
|
|
|
a.appendU64(0x0000000001000000ULL, 15);
|
|
|
|
REPORTER_ASSERT(reporter, a.equals("000000016777216"));
|
2011-02-28 21:29:58 +00:00
|
|
|
|
|
|
|
static const struct {
|
|
|
|
SkScalar fValue;
|
|
|
|
const char* fString;
|
|
|
|
} gRec[] = {
|
|
|
|
{ 0, "0" },
|
|
|
|
{ SK_Scalar1, "1" },
|
|
|
|
{ -SK_Scalar1, "-1" },
|
|
|
|
{ SK_Scalar1/2, "0.5" },
|
2015-08-07 00:56:13 +00:00
|
|
|
#if defined(SK_BUILD_FOR_WIN) && (_MSC_VER < 1900)
|
2011-07-08 17:49:22 +00:00
|
|
|
{ 3.4028234e38f, "3.4028235e+038" },
|
|
|
|
{ -3.4028234e38f, "-3.4028235e+038" },
|
|
|
|
#else
|
2011-03-01 15:44:08 +00:00
|
|
|
{ 3.4028234e38f, "3.4028235e+38" },
|
|
|
|
{ -3.4028234e38f, "-3.4028235e+38" },
|
2011-07-08 17:49:22 +00:00
|
|
|
#endif
|
2011-02-28 21:29:58 +00:00
|
|
|
};
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
|
|
|
|
a.reset();
|
|
|
|
a.appendScalar(gRec[i].fValue);
|
2011-03-01 15:44:08 +00:00
|
|
|
REPORTER_ASSERT(reporter, a.size() <= SkStrAppendScalar_MaxSize);
|
2015-08-07 00:56:13 +00:00
|
|
|
if (!a.equals(gRec[i].fString)) {
|
|
|
|
ERRORF(reporter, "received <%s> expected <%s>\n", a.c_str(), gRec[i].fString);
|
|
|
|
}
|
2011-02-28 21:29:58 +00:00
|
|
|
}
|
2011-06-30 14:39:52 +00:00
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, SkStringPrintf("%i", 0).equals("0"));
|
2011-07-08 17:49:22 +00:00
|
|
|
|
|
|
|
char buffer [40];
|
|
|
|
memset(buffer, 'a', 40);
|
|
|
|
REPORTER_ASSERT(reporter, buffer[18] == 'a');
|
|
|
|
REPORTER_ASSERT(reporter, buffer[19] == 'a');
|
|
|
|
REPORTER_ASSERT(reporter, buffer[20] == 'a');
|
|
|
|
printfAnalog(buffer, 20, "%30d", 0);
|
|
|
|
REPORTER_ASSERT(reporter, buffer[18] == ' ');
|
|
|
|
REPORTER_ASSERT(reporter, buffer[19] == 0);
|
|
|
|
REPORTER_ASSERT(reporter, buffer[20] == 'a');
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2009-02-27 22:06:06 +00:00
|
|
|
}
|
|
|
|
|
2013-12-02 13:50:38 +00:00
|
|
|
DEF_TEST(String_SkStrSplit, r) {
|
|
|
|
SkTArray<SkString> results;
|
|
|
|
|
|
|
|
SkStrSplit("a-_b_c-dee--f-_-_-g-", "-_", &results);
|
|
|
|
REPORTER_ASSERT(r, results.count() == 6);
|
|
|
|
REPORTER_ASSERT(r, results[0].equals("a"));
|
|
|
|
REPORTER_ASSERT(r, results[1].equals("b"));
|
|
|
|
REPORTER_ASSERT(r, results[2].equals("c"));
|
|
|
|
REPORTER_ASSERT(r, results[3].equals("dee"));
|
|
|
|
REPORTER_ASSERT(r, results[4].equals("f"));
|
|
|
|
REPORTER_ASSERT(r, results[5].equals("g"));
|
|
|
|
}
|