From 4aa233d66405791aef44d47c6fc97b561a472528 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 6 Apr 2017 04:59:53 -0500 Subject: [PATCH] Add ox_memcmp. --- src/ox/std/CMakeLists.txt | 4 ++++ src/ox/std/memops.cpp | 16 +++++++++++++++ src/ox/std/memops.hpp | 2 ++ src/ox/std/test/CMakeLists.txt | 10 ++++++++++ src/ox/std/test/tests.cpp | 36 ++++++++++++++++++++++++++++++++++ 5 files changed, 68 insertions(+) create mode 100644 src/ox/std/test/CMakeLists.txt create mode 100644 src/ox/std/test/tests.cpp diff --git a/src/ox/std/CMakeLists.txt b/src/ox/std/CMakeLists.txt index efb6250b8..c2bba8567 100644 --- a/src/ox/std/CMakeLists.txt +++ b/src/ox/std/CMakeLists.txt @@ -15,3 +15,7 @@ install( DESTINATION include/ox/std ) + +if(OX_BUILD_EXEC STREQUAL "ON") + add_subdirectory(test) +endif() diff --git a/src/ox/std/memops.cpp b/src/ox/std/memops.cpp index b58713f37..f6641e30c 100644 --- a/src/ox/std/memops.cpp +++ b/src/ox/std/memops.cpp @@ -7,6 +7,22 @@ */ #include "memops.hpp" +int ox_memcmp(const void *ptr1, const void *ptr2, size_t size) { + int retval = 0; + auto block1 = ((uint8_t*) ptr1); + auto block2 = ((uint8_t*) ptr2); + for (size_t i = 0; i < size; i++) { + if (block1[i] < block2[i]) { + retval = -1; + break; + } else if (block1[i] > block2[i]) { + retval = 1; + break; + } + } + return retval; +} + void *ox_memcpy(void *dest, const void *src, int64_t size) { char *srcBuf = (char*) src; char *dstBuf = (char*) dest; diff --git a/src/ox/std/memops.hpp b/src/ox/std/memops.hpp index d6b1dc9e1..810f0fca5 100644 --- a/src/ox/std/memops.hpp +++ b/src/ox/std/memops.hpp @@ -9,6 +9,8 @@ #include "types.hpp" +int ox_memcmp(const void *ptr1, const void *ptr2, size_t size); + void *ox_memcpy(void *src, const void *dest, int64_t size); void *ox_memset(void *ptr, int val, int64_t size); diff --git a/src/ox/std/test/CMakeLists.txt b/src/ox/std/test/CMakeLists.txt new file mode 100644 index 000000000..31ef13785 --- /dev/null +++ b/src/ox/std/test/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 2.8) + +add_executable( + StdTest + tests.cpp +) + +target_link_libraries(StdTest OxStd) + +add_test("Test\\ ox_memcmp" StdTest "ox_memcmp") diff --git a/src/ox/std/test/tests.cpp b/src/ox/std/test/tests.cpp new file mode 100644 index 000000000..131560b34 --- /dev/null +++ b/src/ox/std/test/tests.cpp @@ -0,0 +1,36 @@ +/* + * Copyright 2015 - 2016 gtalent2@gmail.com + * + * 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 +#include +#include +#include + +::std::map> tests = { + { + "ox_memcmp", + []() { + int success = 1; + const char *data1 = "ABCDEFG"; + const char *data2 = "HIJKLMN"; + success &= ox_memcmp(data1, data2, 7) < 0; + success &= ox_memcmp(data2, data1, 7) > 0; + success &= ox_memcmp(data1, data1, 7) == 0; + return !success; + } + } +}; + +int main(int argc, const char **args) { + if (argc > 1) { + auto testName = args[1]; + if (tests.find(testName) != tests.end()) { + return tests[testName](); + } + } + return -1; +}