From 0a068dfea48f91c4590a88f58081254c72ca282f Mon Sep 17 00:00:00 2001 From: Zach DeCook Date: Sat, 27 Jun 2020 10:15:23 -0400 Subject: [PATCH] * I8, I16, I32, I64: Implement mess --- src/I.php | 25 +++++++++++++++++++++++++ src/I16.php | 1 + src/I32.php | 1 + src/I64.php | 1 + src/I8.php | 1 + src/Is.php | 8 ++++++++ tests/PrimitivesTest.php | 27 +++++++++++++++++++++++++++ 7 files changed, 64 insertions(+) create mode 100644 src/I.php create mode 120000 src/I16.php create mode 120000 src/I32.php create mode 120000 src/I64.php create mode 120000 src/I8.php create mode 100644 src/Is.php diff --git a/src/I.php b/src/I.php new file mode 100644 index 0000000..12ffe95 --- /dev/null +++ b/src/I.php @@ -0,0 +1,25 @@ +value <= 0) { + // chr makes binary string of least significant byte. + $mess .= chr(abs($this->value) >> ($i * 8)); + } else { + $mess .= chr(0xFF - (($this->value - 1) >> ($i * 8)) ); + } + } + return $mess; + } +} diff --git a/src/I16.php b/src/I16.php new file mode 120000 index 0000000..c93c919 --- /dev/null +++ b/src/I16.php @@ -0,0 +1 @@ +Is.php \ No newline at end of file diff --git a/src/I32.php b/src/I32.php new file mode 120000 index 0000000..c93c919 --- /dev/null +++ b/src/I32.php @@ -0,0 +1 @@ +Is.php \ No newline at end of file diff --git a/src/I64.php b/src/I64.php new file mode 120000 index 0000000..c93c919 --- /dev/null +++ b/src/I64.php @@ -0,0 +1 @@ +Is.php \ No newline at end of file diff --git a/src/I8.php b/src/I8.php new file mode 120000 index 0000000..c93c919 --- /dev/null +++ b/src/I8.php @@ -0,0 +1 @@ +Is.php \ No newline at end of file diff --git a/src/Is.php b/src/Is.php new file mode 100644 index 0000000..3b782fe --- /dev/null +++ b/src/Is.php @@ -0,0 +1,8 @@ +assertEquals($value, $pk3->get()); } + public function testIs() + { + $i8 = new I8(); + $this->assertEquals("\0", $i8->mess()); + $i8->set(1); + $this->assertEquals("\xFF", $i8->mess()); + $i8->set(-1); + $this->assertEquals("\x01", $i8->mess()); + + $i64 = new I64(); + $this->assertEquals(0, $i64->get()); + // Largest representable value. + $i64->set(0x7FFFFFFFFFFFFFFF); + // Twos complement max int looks like 0b1000...0001, + // note that this is little endian. + $this->assertEquals("\x01\0\0\0\0\0\0\x80", $i64->mess()); + + // Smallest representable value + $i64->set(-1 << 63); + $this->assertEquals(-0x7FFFFFFFFFFFFFFF - 1, $i64->get()); + $this->assertEquals("\0\0\0\0\0\0\0\x80", $i64->mess()); + + $i64->set(1); + $this->assertEquals("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", $i64->mess()); + } + public function testUs() { $u8 = new U8(); -- 2.45.2