summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNiko Lehto <nikle@cendio.se>2020-01-28 17:00:04 +0100
committerNiko Lehto <nikle@cendio.se>2020-02-17 11:29:40 +0100
commit9575ded8da83b6d8774b36316c388279fa0512cc (patch)
treeeba8d0366bb1eee4351267a1aa2840815a90b956
parentf52e979082926ab535f36f33e29693988a4bdfef (diff)
downloadnovnc-9575ded8da83b6d8774b36316c388279fa0512cc.tar.gz
Add util for unsigned and signed int. conversion
Will be used in later commit in extended clipboard handling.
-rw-r--r--core/util/int.js15
-rw-r--r--tests/test.int.js16
2 files changed, 31 insertions, 0 deletions
diff --git a/core/util/int.js b/core/util/int.js
new file mode 100644
index 0000000..001f40f
--- /dev/null
+++ b/core/util/int.js
@@ -0,0 +1,15 @@
+/*
+ * noVNC: HTML5 VNC client
+ * Copyright (C) 2020 The noVNC Authors
+ * Licensed under MPL 2.0 (see LICENSE.txt)
+ *
+ * See README.md for usage and integration instructions.
+ */
+
+export function toUnsigned32bit(toConvert) {
+ return toConvert >>> 0;
+}
+
+export function toSigned32bit(toConvert) {
+ return toConvert | 0;
+}
diff --git a/tests/test.int.js b/tests/test.int.js
new file mode 100644
index 0000000..954fd27
--- /dev/null
+++ b/tests/test.int.js
@@ -0,0 +1,16 @@
+/* eslint-disable no-console */
+const expect = chai.expect;
+
+import { toUnsigned32bit, toSigned32bit } from '../core/util/int.js';
+
+describe('Integer casting', function () {
+ it('should cast unsigned to signed', function () {
+ let expected = 4294967286;
+ expect(toUnsigned32bit(-10)).to.equal(expected);
+ });
+
+ it('should cast signed to unsigned', function () {
+ let expected = -10;
+ expect(toSigned32bit(4294967286)).to.equal(expected);
+ });
+});