Add ox_memcmp.
This commit is contained in:
@@ -15,3 +15,7 @@ install(
|
||||
DESTINATION
|
||||
include/ox/std
|
||||
)
|
||||
|
||||
if(OX_BUILD_EXEC STREQUAL "ON")
|
||||
add_subdirectory(test)
|
||||
endif()
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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")
|
||||
@@ -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 <iostream>
|
||||
#include <map>
|
||||
#include <functional>
|
||||
#include <ox/std/std.hpp>
|
||||
|
||||
::std::map<std::string, std::function<int()>> 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;
|
||||
}
|
||||
Reference in New Issue
Block a user