Added some functionalities for the test framework
authorBenjamin Segovia <segovia.benjamin@gmail.com>
Thu, 3 May 2012 14:50:39 +0000 (14:50 +0000)
committerKeith Packard <keithp@keithp.com>
Fri, 10 Aug 2012 23:16:54 +0000 (16:16 -0700)
33 files changed:
CMakeLists.txt
tests/CMakeLists.txt [new file with mode: 0644]
tests/assert.cpp [new file with mode: 0644]
tests/assert.hpp [new file with mode: 0644]
tests/exception.hpp [new file with mode: 0644]
tests/to_update/aes.c [moved from tests/aes.c with 100% similarity]
tests/to_update/binomialOption.c [moved from tests/binomialOption.c with 100% similarity]
tests/to_update/bitonicSort.c [moved from tests/bitonicSort.c with 100% similarity]
tests/to_update/blackscholes.c [moved from tests/blackscholes.c with 100% similarity]
tests/to_update/dct.c [moved from tests/dct.c with 100% similarity]
tests/to_update/fastWalsh.c [moved from tests/fastWalsh.c with 100% similarity]
tests/to_update/fft.c [moved from tests/fft.c with 100% similarity]
tests/to_update/mandelbrot.c [moved from tests/mandelbrot.c with 100% similarity]
tests/to_update/matmul.c [moved from tests/matmul.c with 100% similarity]
tests/to_update/mersenneTwister.c [moved from tests/mersenneTwister.c with 100% similarity]
tests/to_update/monteCarloAsian.c [moved from tests/monteCarloAsian.c with 100% similarity]
tests/to_update/nbody.c [moved from tests/nbody.c with 100% similarity]
tests/to_update/svm_test.c [moved from tests/svm_test.c with 100% similarity]
tests/to_update/test_2d_copy.c [moved from tests/test_2d_copy.c with 100% similarity]
tests/to_update/test_barrier.c [moved from tests/test_barrier.c with 100% similarity]
tests/to_update/test_constant_memory.c [moved from tests/test_constant_memory.c with 100% similarity]
tests/to_update/test_copy_image.c [moved from tests/test_copy_image.c with 100% similarity]
tests/to_update/test_enqueue_read.c [moved from tests/test_enqueue_read.c with 100% similarity]
tests/to_update/test_imm_parameters.c [moved from tests/test_imm_parameters.c with 100% similarity]
tests/to_update/test_local_memory.c [moved from tests/test_local_memory.c with 100% similarity]
tests/to_update/test_memory_leak.c [moved from tests/test_memory_leak.c with 100% similarity]
tests/to_update/test_private_memory.c [moved from tests/test_private_memory.c with 100% similarity]
tests/to_update/test_static_local_memory.c [moved from tests/test_static_local_memory.c with 100% similarity]
tests/to_update/transpose.c [moved from tests/transpose.c with 100% similarity]
tests/to_update/urng.c [moved from tests/urng.c with 100% similarity]
tests/to_update/vadd.c [moved from tests/vadd.c with 100% similarity]
tests/utest.cpp [new file with mode: 0644]
tests/utest.hpp [new file with mode: 0644]

index 73de2a2..a7548d2 100644 (file)
@@ -45,7 +45,7 @@ ELSE (USE_FULSIM)
   ADD_DEFINITIONS(-DUSE_FULSIM=0)
 ENDIF (USE_FULSIM)
 
-SET(CMAKE_CXX_FLAGS "-Wall -Wno-invalid-offsetof -mfpmath=sse --no-exceptions --no-rtti -Wcast-align -std=c++0x")
+SET(CMAKE_CXX_FLAGS "-Wall -Wno-invalid-offsetof -mfpmath=sse --no-rtti -Wcast-align -std=c++0x")
 SET(CMAKE_C_FLAGS "-Wall -mfpmath=sse -msse2 -Wcast-align")
 
 # XLib
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
new file mode 100644 (file)
index 0000000..ac97133
--- /dev/null
@@ -0,0 +1,12 @@
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}
+                    ${CMAKE_CURRENT_SOURCE_DIR}/../include)
+
+ADD_LIBRARY(cl_test SHARED
+            cl_test.c
+            cl_file_map.c
+            common.c
+            assert.cpp
+            utest.cpp)
+ADD_EXECUTABLE(write_only write_only.c)
+TARGET_LINK_LIBRARIES(write_only cl m cl_test)
+
diff --git a/tests/assert.cpp b/tests/assert.cpp
new file mode 100644 (file)
index 0000000..d3c1f56
--- /dev/null
@@ -0,0 +1,42 @@
+/* 
+ * Copyright © 2012 Intel Corporation
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Benjamin Segovia <benjamin.segovia@intel.com>
+ */
+
+/**
+ * \file assert.cpp
+ * \author Benjamin Segovia <benjamin.segovia@intel.com>
+ */
+#include "assert.hpp"
+#include "exception.hpp"
+#include <cassert>
+#include <cstdlib>
+
+void onFailedAssertion(const char *msg, const char *file, const char *fn, int line)
+{
+  char lineString[256];
+  sprintf(lineString, "%i", line);
+  assert(msg != NULL && file != NULL && fn != NULL);
+  const std::string str = "Compiler error: "
+                        + std::string(msg) + "\n  at file "
+                        + std::string(file)
+                        + ", function " + std::string(fn)
+                        + ", line " + std::string(lineString);
+  // assert(0);
+  throw Exception(str);
+}
+
diff --git a/tests/assert.hpp b/tests/assert.hpp
new file mode 100644 (file)
index 0000000..d124e9f
--- /dev/null
@@ -0,0 +1,32 @@
+/* 
+ * Copyright © 2012 Intel Corporation
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Benjamin Segovia <benjamin.segovia@intel.com>
+ */
+
+/**
+ * \file assert.hpp
+ *
+ * \author Benjamin Segovia <benjamin.segovia@intel.com>
+ */
+#ifndef __OCL_ASSERT_HPP__
+#define __OCL_ASSERT_HPP__
+
+/*! To ensure that condition truth. Optional message is supported */
+void onFailedAssertion(const char *msg, const char *file, const char *fn, int line);
+
+#endif /* __OCL_ASSERT_HPP__ */
+
diff --git a/tests/exception.hpp b/tests/exception.hpp
new file mode 100644 (file)
index 0000000..7e28bdb
--- /dev/null
@@ -0,0 +1,48 @@
+/* 
+ * Copyright © 2012 Intel Corporation
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Benjamin Segovia <benjamin.segovia@intel.com>
+ */
+
+/**
+ * \file exception.hpp
+ *
+ * \author Benjamin Segovia <benjamin.segovia@intel.com>
+ */
+#ifndef __OCL_EXCEPTION_HPP__
+#define __OCL_EXCEPTION_HPP__
+
+#include <string>
+#include <exception>
+
+/*! Exception are only used while using unit tests */
+class Exception : public std::exception
+{
+public:
+  Exception(const std::string &msg) throw() : msg(msg) {}
+  Exception(const Exception &other) throw() : msg(other.msg) {}
+  ~Exception(void) throw() {}
+  Exception &operator= (const Exception &other) throw() {
+    this->msg = other.msg;
+    return *this;
+  }
+  const char *what(void) const throw() { return msg.c_str(); }
+private:
+  std::string msg; //!< String message
+};
+
+#endif /* __OCL_EXCEPTION_HPP__ */
+
similarity index 100%
rename from tests/aes.c
rename to tests/to_update/aes.c
similarity index 100%
rename from tests/dct.c
rename to tests/to_update/dct.c
similarity index 100%
rename from tests/fft.c
rename to tests/to_update/fft.c
similarity index 100%
rename from tests/matmul.c
rename to tests/to_update/matmul.c
similarity index 100%
rename from tests/nbody.c
rename to tests/to_update/nbody.c
similarity index 100%
rename from tests/svm_test.c
rename to tests/to_update/svm_test.c
similarity index 100%
rename from tests/urng.c
rename to tests/to_update/urng.c
similarity index 100%
rename from tests/vadd.c
rename to tests/to_update/vadd.c
diff --git a/tests/utest.cpp b/tests/utest.cpp
new file mode 100644 (file)
index 0000000..421c3b6
--- /dev/null
@@ -0,0 +1,73 @@
+/* 
+ * Copyright © 2012 Intel Corporation
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Benjamin Segovia <benjamin.segovia@intel.com>
+ */
+
+/**
+ * \file utest.cpp
+ * \author Benjamin Segovia <benjamin.segovia@intel.com>
+ */
+#include "utest.hpp"
+#include <vector>
+#include <string>
+#include <iostream>
+#include <cstring>
+
+using namespace std;
+vector<UTest> *UTest::utestList = NULL;
+void releaseUTestList(void) { delete UTest::utestList; }
+
+UTest::UTest(Function fn, const char *name) : fn(fn), name(name) {
+  if (utestList == NULL) {
+    utestList = new vector<UTest>;
+    atexit(releaseUTestList);
+  }
+  utestList->push_back(*this);
+}
+
+UTest::UTest(void) : fn(NULL), name(NULL) {}
+
+static bool strequal(const char *s1, const char *s2) {
+  if (strcmp(s1, s2) == 0) return true;
+  return false;
+}
+
+void UTest::run(const char *name) {
+  if (name == NULL) return;
+  if (utestList == NULL) return;
+  for (size_t i = 0; i < utestList->size(); ++i) {
+    const UTest &utest = (*utestList)[i];
+    if (utest.name == NULL || utest.fn == NULL) continue;
+    if (strequal(utest.name, name)) {
+      std::cout << utest.name << ":" << std::endl;
+      (utest.fn)();
+      std::cout << std::endl;
+    }
+  }
+}
+
+void UTest::runAll(void) {
+  if (utestList == NULL) return;
+  for (size_t i = 0; i < utestList->size(); ++i) {
+    const UTest &utest = (*utestList)[i];
+    if (utest.fn == NULL) continue;
+    std::cout << utest.name << ":" << std::endl;
+    (utest.fn)();
+    std::cout << std::endl;
+  }
+}
+
diff --git a/tests/utest.hpp b/tests/utest.hpp
new file mode 100644 (file)
index 0000000..30b80b9
--- /dev/null
@@ -0,0 +1,82 @@
+/* 
+ * Copyright © 2012 Intel Corporation
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Benjamin Segovia <benjamin.segovia@intel.com>
+ */
+
+/**
+ * \file utest.hpp
+ * \author Benjamin Segovia <benjamin.segovia@intel.com>
+ *
+ * Provides all unit test capabilites. It is rather rudimentary but it should
+ * do the job
+ */
+#ifndef __OCL_UTEST_HPP__
+#define __OCL_UTEST_HPP__
+
+#include <vector>
+#include <exception>
+
+/*! Quick and dirty unit test system with registration */
+struct UTest
+{
+  /*! A unit test function to run */
+  typedef void (*Function) (void);
+  /*! Empty test */
+  UTest(void);
+  /*! Build a new unit test and append it to the unit test list */
+  UTest(Function fn, const char *name);
+  /*! Function to execute */
+  Function fn;
+  /*! Name of the test */
+  const char *name;
+  /*! The tests that are registered */
+  static std::vector<UTest> *utestList;
+  /*! Run the test with the given name */
+  static void run(const char *name);
+  /*! Run all the tests */
+  static void runAll(void);
+};
+
+/*! RegisterData a new unit test */
+#define UTEST_REGISTER(FN) static const UTest __##FN##__(FN, #FN);
+
+/*! No assert is expected */
+#define UTEST_EXPECT_SUCCESS(EXPR) \
+ do { \
+    try { \
+      EXPR; \
+      std::cout << "  " << #EXPR << "    [SUCCESS]" << std::endl; \
+    } \
+    catch (gbe::Exception e) { \
+      std::cout << "  " << #EXPR << "    [FAILED]" << std::endl; \
+      std::cout << "    " << e.what() << std::endl; \
+    } \
+  } while (0)
+
+#define UTEST_EXPECT_FAILED(EXPR) \
+ do { \
+    try { \
+      EXPR; \
+      std::cout << "  " << #EXPR << "    [FAILED]" <<  std::endl; \
+    } \
+    catch (gbe::Exception e) { \
+      std::cout << "  " << #EXPR << "    [SUCCESS]" << std::endl; \
+    } \
+  } while (0)
+
+#endif /* __OCL_UTEST_HPP__ */
+