Bug 553032 - add unit tests for JS_smprintf; r=evilpie
authorTom Tromey <tom@tromey.com>
Thu, 13 Oct 2016 14:39:09 -0600
changeset 318977 482223f975509866e1c2502fbc1ba6c56cc5c97d
parent 318976 f6c702f47a7e7eddbc7fc0449dccf033c14ef1d6
child 318978 a12133c22f121112c72c12dd0866ec4b3aabb31a
push id83047
push userryanvm@gmail.com
push dateFri, 21 Oct 2016 21:15:09 +0000
treeherdermozilla-inbound@32f298a71ce9 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersevilpie
bugs553032
milestone52.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 553032 - add unit tests for JS_smprintf; r=evilpie MozReview-Commit-ID: 4WDp1k839Si
js/src/jsapi-tests/moz.build
js/src/jsapi-tests/testPrintf.cpp
--- a/js/src/jsapi-tests/moz.build
+++ b/js/src/jsapi-tests/moz.build
@@ -68,16 +68,17 @@ UNIFIED_SOURCES += [
     'testNewObject.cpp',
     'testNewTargetInvokeConstructor.cpp',
     'testNullRoot.cpp',
     'testObjectEmulatingUndefined.cpp',
     'testOOM.cpp',
     'testParseJSON.cpp',
     'testPersistentRooted.cpp',
     'testPreserveJitCode.cpp',
+    'testPrintf.cpp',
     'testPrivateGCThingValue.cpp',
     'testProfileStrings.cpp',
     'testPropCache.cpp',
     'testRegExp.cpp',
     'testResolveRecursion.cpp',
     'tests.cpp',
     'testSameValue.cpp',
     'testSavedStacks.cpp',
@@ -141,16 +142,16 @@ if CONFIG['ENABLE_INTL_API'] and CONFIG[
 
 USE_LIBS += [
     'static:js',
 ]
 
 OS_LIBS += CONFIG['MOZ_ZLIB_LIBS']
 
 if CONFIG['GNU_CXX']:
-    CXXFLAGS += ['-Wno-shadow']
+    CXXFLAGS += ['-Wno-shadow', '-Werror=format']
 
 # This is intended as a temporary workaround to enable VS2015.
 if CONFIG['_MSC_VER']:
     CXXFLAGS += ['-wd4312']
 
 DEFINES['topsrcdir'] = '%s/js/src' % TOPSRCDIR
 OBJDIR_PP_FILES.js.src['jsapi-tests'] += ['jsapi-tests-gdb.py.in']
new file mode 100644
--- /dev/null
+++ b/js/src/jsapi-tests/testPrintf.cpp
@@ -0,0 +1,71 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+ * vim: set ts=8 sts=4 et sw=4 tw=99:
+ */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "mozilla/IntegerPrintfMacros.h"
+#include "mozilla/SizePrintfMacros.h"
+
+#include <stdarg.h>
+
+#include "jsprf.h"
+
+#include "jsapi-tests/tests.h"
+
+static bool
+MOZ_FORMAT_PRINTF(2, 3)
+print_one (const char *expect, const char *fmt, ...)
+{
+    va_list ap;
+
+    va_start(ap, fmt);
+    char *output = JS_vsmprintf (fmt, ap);
+    va_end(ap);
+
+    bool result = output && !strcmp(output, expect);
+    JS_smprintf_free(output);
+
+    return result;
+}
+
+static const char *
+zero()
+{
+    return nullptr;
+}
+
+BEGIN_TEST(testPrintf)
+{
+    CHECK(print_one("23", "%d", 23));
+    CHECK(print_one("-1", "%d", -1));
+    CHECK(print_one("23", "%u", 23u));
+    CHECK(print_one("0x17", "0x%x", 23u));
+    CHECK(print_one("0xFF", "0x%X", 255u));
+    CHECK(print_one("027", "0%o", 23u));
+    CHECK(print_one("-1", "%hd", (short) -1));
+    // This could be expanded if need be, it's just convenient to do
+    // it this way.
+    if (sizeof(short) == 2) {
+        CHECK(print_one("8000", "%hx", (unsigned short) 0x8000));
+    }
+    CHECK(print_one("0xf0f0", "0x%lx", 0xf0f0ul));
+    CHECK(print_one("0xF0F0", "0x%llX", 0xf0f0ull));
+    CHECK(print_one("27270", "%zu", (size_t) 27270));
+    CHECK(print_one("27270", "%" PRIuSIZE, (size_t) 27270));
+    CHECK(print_one("hello", "he%so", "ll"));
+    CHECK(print_one("(null)", "%s", zero()));
+    CHECK(print_one("0", "%p", (char *) 0));
+    CHECK(print_one("h", "%c", 'h'));
+    CHECK(print_one("1.500000", "%f", 1.5f));
+    CHECK(print_one("1.5", "%g", 1.5));
+
+    CHECK(print_one("2727", "%" PRIu32, (uint32_t) 2727));
+    CHECK(print_one("aa7", "%" PRIx32, (uint32_t) 2727));
+    CHECK(print_one("2727", "%" PRIu64, (uint64_t) 2727));
+    CHECK(print_one("aa7", "%" PRIx64, (uint64_t) 2727));
+
+    return true;
+}
+END_TEST(testPrintf)