Website Summary Files Commits Branches @commit @diff @stats
path: root / JSImpl / test
diff options
context:
space:
mode:
Commit0b2444013dfb6bfb0c033b5b827610f6df49ede5 (patch)
Author
 dtrendafilov <trendafilov.dn@gmail.com>
2018-11-25 09:08:47 +0200
Committer
 GitHub <noreply@github.com>
2018-11-25 09:08:47 +0200
Filese6206cd3329067cd48dd0ccf90262f6eaa6cdbbb /JSImpl/test
IPL-0b2444013dfb6bfb0c033b5b827610f6df49ede5.tar
IPL-0b2444013dfb6bfb0c033b5b827610f6df49ede5.tar.gz
IPL-0b2444013dfb6bfb0c033b5b827610f6df49ede5.zip
Parent156d0957c1b36bba2685fef548a8573e9337309a (diff)
Parent4fca8e8e6c1056aed11437c87e9175232b48c8f4 (diff)
Merge branch 'master' into json-to-ast
2 files changed, 135 insertions, 19 deletions
-rw-r--r--JSImpl/test/empty.cpp116
-rw-r--r--JSImpl/test/sprtTests.cpp38
diff --git a/JSImpl/test/empty.cpp b/JSImpl/test/empty.cpp
index 2787b00..2725467 100644
--- a/JSImpl/test/empty.cpp
+++ b/JSImpl/test/empty.cpp
@@ -1,4 +1,6 @@
#include <gtest/gtest.h>
+#include <memory>
+#include <cmath>
TEST(Empty, Empty)
{
@@ -58,5 +60,119 @@ TEST(Pointer, int64)
ASSERT_EQ(data + 9, PC);
}
+/*
+ * Value will be either a double, undefined, null, string, array, object,
+ * function, boolean.
+ *
+ * Pointer values (like string, array, object, function) can be shared pointers
+ * via intrusive pointer implementation. This way we can get a reference
+ * counting garbage collection.
+ *
+ */
+struct Value
+{
+ Value(double v)
+ {
+ m_value.as_double = v;
+ }
+
+ Value(int tag, void* pointer)
+ {
+ assert(tag && "Zero tag is plain double");
+ assert(!((size_t)pointer >> 48) && "Use only pointers to the heap.");
+ m_value.as_pointer.pointer = (size_t)pointer;
+ m_value.as_pointer.tag = tag;
+ m_value.as_pointer.nan = 0x1fff;
+ }
+ struct NanPointer
+ {
+ size_t pointer:48;
+ size_t tag:3;
+ size_t nan:13;
+ };
+
+ struct CheckType
+ {
+ size_t payload: 48;
+ size_t check:16;
+ };
+ union {
+ double as_double;
+ NanPointer as_pointer;
+ CheckType to_check;
+
+ } m_value;
+
+ static_assert(sizeof(size_t) == 8, "unsupported arch");
+ static_assert(sizeof(double) == 8, "unsupported arch");
+
+ bool is_double() const
+ {
+ return m_value.to_check.check <= 0xFFF8;
+ }
+
+ double get_double() const
+ {
+ assert(is_double());
+ return m_value.as_double;
+ }
+
+ int get_tag() {
+ assert(!is_double());
+ return m_value.as_pointer.tag;
+ }
+
+ void* get_pointer() const {
+ return (void*)(size_t) m_value.as_pointer.pointer;
+ }
+};
+
+
+TEST(NaNBox, PlainDoubles)
+{
+ double a = floor(rand() / RAND_MAX);
+ double values[] = {
+ 0.0, 2.89, 3.14, 0.0 / 1.0, 1.0 / a
+ };
+ for (auto value: values)
+ {
+ Value box(value);
+ ASSERT_TRUE(box.is_double());
+ ASSERT_EQ(value, box.get_double());
+
+ Value box2(-value);
+ ASSERT_TRUE(box2.is_double());
+ ASSERT_EQ(-value, box2.get_double());
+ }
+}
+TEST(NaNBox, Border)
+{
+ Value v(2, nullptr);
+ ASSERT_TRUE(std::isnan(v.m_value.as_double));
+ ASSERT_FALSE(std::isinf(v.m_value.as_double));
+}
+TEST(NaNBox, IsNaN)
+{
+ Value v2(std::sqrt(-1.0));
+ ASSERT_TRUE(v2.is_double());
+}
+
+TEST(NaNBox, Pointers)
+{
+ std::vector<std::unique_ptr<int>> pointers;
+ int n = 1024;
+ pointers.reserve(n);
+ for (int i = 0; i < n; ++i)
+ {
+ pointers.emplace_back(std::make_unique<int>(i));
+ }
+
+ for (auto& value: pointers)
+ {
+ Value box(1, value.get());
+ ASSERT_FALSE(box.is_double());
+ ASSERT_EQ(value.get(), box.get_pointer());
+ }
+}
diff --git a/JSImpl/test/sprtTests.cpp b/JSImpl/test/sprtTests.cpp
index eef7147..8480554 100644
--- a/JSImpl/test/sprtTests.cpp
+++ b/JSImpl/test/sprtTests.cpp
@@ -81,7 +81,7 @@ TEST_F(SPRTTest, Div)
};
Run(bytecode, sizeof(bytecode));
- ASSERT_EQ(Output.str(), "0");
+ ASSERT_EQ(Output.str(), "0.857143");
}
TEST_F(SPRTTest, Mod)
@@ -235,24 +235,24 @@ TEST_F(SPRTTest, Read)
TEST_F(SPASMTest, RSyntax)
{
const char* program =
- "push 5" "\n"
- "const r1 0" "\n"
- "const r2 6" "\n"
- "const r3 7" "\n"
- "const r4 2" "\n"
- "pushr r3" "\n"
- "pushr r2" "\n"
- "pushr r4" "\n"
- "call mult" "\n"
- "popr r1" "\n"
- "print r1" "\n"
- "halt" "\n"
- "label mult" "\n"
- "print a0" "\n"
- "print a1" "\n"
- "print a2" "\n"
- "mul r1 a2 a1" "\n"
- "ret r1" "\n"
+ "00: push 5" "\n"
+ "01: const r1 0" "\n"
+ "02: const r2 6" "\n"
+ "03: const r3 7" "\n"
+ "04: const r4 2" "\n"
+ "05: pushr r3" "\n"
+ "06: pushr r2" "\n"
+ "07: pushr r4" "\n"
+ "08: call mult # *" "\n"
+ "09: popr r1" "\n"
+ "10: print r1" "\n"
+ "11: halt" "\n"
+ "12: label mult" "\n"
+ "13: print a0" "\n"
+ "14: print a1" "\n"
+ "15: print a2" "\n"
+ "16: mul r1 a2 a1" "\n"
+ "17: ret r1" "\n"
""
;
CompileAndRun(program);