[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[tyndur-devel] [PATCH 3/3] + testcases: 18.1 Language support library, Types: <cstddef>



---
 testcase/cstddef/NULL.cpp                  |   34 ++++++++
 testcase/cstdint.cpp                       |  128 ++++++++++++++++++++++++++++
 testcase/cstdlib/quick_exit-exceptions.cpp |   38 ++++++++
 testcase/cstdlib/quick_exit.cpp            |   52 +++++++++++
 testcase/new/bad_alloc.cpp                 |   37 ++++++++
 testcase/new/new_handler.cpp               |   37 ++++++++
 testcase/new/nothrow.cpp                   |   31 +++++++
 7 files changed, 357 insertions(+), 0 deletions(-)
 create mode 100644 testcase/cstddef/NULL.cpp
 create mode 100644 testcase/cstdint.cpp
 create mode 100644 testcase/cstdlib/quick_exit-exceptions.cpp
 create mode 100644 testcase/cstdlib/quick_exit.cpp
 create mode 100644 testcase/new/bad_alloc.cpp
 create mode 100644 testcase/new/new_handler.cpp
 create mode 100644 testcase/new/nothrow.cpp

diff --git a/testcase/cstddef/NULL.cpp b/testcase/cstddef/NULL.cpp
new file mode 100644
index 0000000..11e8c42
--- /dev/null
+++ b/testcase/cstddef/NULL.cpp
@@ -0,0 +1,34 @@
+/*
+Permission is granted to use, modify, and / or redistribute at will.
+
+This includes removing authorship notices, re-use of code parts in
+other software (with or without giving credit), and / or creating a
+commercial product based on it.
+
+This permission is not revocable by the author.
+
+This software is provided as-is. Use it at your own risk. There is
+no warranty whatsoever, neither expressed nor implied, and by using
+this software you accept that the author(s) shall not be held liable
+for any loss of data, loss of service, or other damages, be they
+incidental or consequential. Your only option other than accepting
+this is not to use the software at all.
+*/
+#include <cstddef>
+#include <cstdlib>
+#include <typeinfo>
+
+/*
+ * Checks that the type of NULL is not void*. If this test fails your libc is
+ * at fault and should be fixed. You have to define NULL to an
+ * "implementation-defined C++ null pointer constant". "Possible definitions
+ * include 0 and OL, but not (void*)0" (Kapitel 18.1 ISO/IEC 14882:2003).
+ */
+
+int main()
+{
+  if (typeid(NULL) == typeid(void*))
+    return EXIT_FAILURE;
+
+  return EXIT_SUCCESS;
+}
diff --git a/testcase/cstdint.cpp b/testcase/cstdint.cpp
new file mode 100644
index 0000000..f2d65f3
--- /dev/null
+++ b/testcase/cstdint.cpp
@@ -0,0 +1,128 @@
+/*
+Permission is granted to use, modify, and / or redistribute at will.
+
+This includes removing authorship notices, re-use of code parts in
+other software (with or without giving credit), and / or creating a
+commercial product based on it.
+
+This permission is not revocable by the author.
+
+This software is provided as-is. Use it at your own risk. There is
+no warranty whatsoever, neither expressed nor implied, and by using
+this software you accept that the author(s) shall not be held liable
+for any loss of data, loss of service, or other damages, be they
+incidental or consequential. Your only option other than accepting
+this is not to use the software at all.
+*/
+#define __STDC_LIMIT_MACROS            1
+#define __STDC_CONSTANT_MACROS         1
+#include <typeinfo>
+#include <cstdint>
+#include <cstdlib>
+// TODO: #include <cassert> // assert
+#include <assert.h>
+// TODO: #include <climits> // CHAR_BIT
+#include <limits.h>
+
+/*
+ * Checks whether <cstdint> complies to the C Standard
+ */
+
+template<typename T,
+         T base,
+         unsigned int exponent>
+struct power
+{
+  static const T value = base * power<T, base, exponent - 1>::value;
+};
+
+template<typename T,
+         T base>
+struct power<T, base, 0>
+{
+  static const T value = 1;
+};
+
+template<unsigned int exponent>
+struct signed_power_of_2
+{
+  static const std::intmax_t value = power<std::intmax_t, 2, exponent>::value;
+};
+
+template<unsigned int exponent>
+struct unsigned_power_of_2
+{
+  static const std::uintmax_t value = power<std::uintmax_t, 2, exponent>::value;
+};
+
+#define ASSERT_TYPE_OF_SIZE(x)         assert(sizeof(std::int##x##_t) * CHAR_BIT == x); \
+                                       assert(sizeof(std::uint##x##_t) * CHAR_BIT == x); \
+                                       assert(sizeof(std::int_least##x##_t) * CHAR_BIT >= x); \
+                                       assert(sizeof(std::uint_least##x##_t) * CHAR_BIT >= x); \
+                                       assert(sizeof(std::int_fast##x##_t) * CHAR_BIT >= x); \
+                                       assert(sizeof(std::uint_fast##x##_t) * CHAR_BIT >= x);
+#define ASSERT_SIZE_MACRO(x)           assert(INT##x##_MIN == - signed_power_of_2<x - 1>::value); \
+                                       assert(INT##x##_MAX == signed_power_of_2<x - 1>::value - 1); \
+                                       assert(UINT##x##_MAX == unsigned_power_of_2<x>::value - 1); \
+                                       assert(INT_LEAST##x##_MIN <= - (signed_power_of_2<x - 1>::value - 1)); \
+                                       assert(INT_LEAST##x##_MAX >= signed_power_of_2<x - 1>::value - 1); \
+                                       assert(UINT_LEAST##x##_MAX >= unsigned_power_of_2<x>::value - 1); \
+                                       assert(INT_FAST##x##_MIN <= - (signed_power_of_2<x - 1>::value - 1)); \
+                                       assert(INT_FAST##x##_MAX >= signed_power_of_2<x - 1>::value - 1); \
+                                       assert(UINT_FAST##x##_MAX >= unsigned_power_of_2<x>::value - 1);
+
+int i;
+
+int main()
+{
+  /*
+   * assertions for the sizes of the new types
+   */
+  ASSERT_TYPE_OF_SIZE(8);
+  ASSERT_TYPE_OF_SIZE(16);
+  ASSERT_TYPE_OF_SIZE(32);
+  ASSERT_TYPE_OF_SIZE(64);
+
+  void* p = &i;
+  assert(reinterpret_cast<void*>(reinterpret_cast<std::intptr_t>(p)) == p);
+  assert(reinterpret_cast<void*>(reinterpret_cast<std::uintptr_t>(p)) == p);
+
+
+
+  /*
+   * assertions for the type size macros
+   */
+  ASSERT_SIZE_MACRO(8);
+  ASSERT_SIZE_MACRO(16);
+  ASSERT_SIZE_MACRO(32);
+  assert(INT64_MIN == (signed long long)-0x8000000000000000LL);
+  assert(INT64_MAX == 0x7FFFFFFFFFFFFFFFLL);
+  assert(UINT64_MAX == 0xFFFFFFFFFFFFFFFFULL);
+  assert(INT_LEAST64_MIN <= -0x7FFFFFFFFFFFFFFFLL);
+  assert(INT_LEAST64_MAX >= 0x7FFFFFFFFFFFFFFFLL);
+  assert(UINT_LEAST64_MAX >= 0xFFFFFFFFFFFFFFFFULL);
+  assert(INT_FAST64_MIN <= -0x7FFFFFFFFFFFFFFFLL);
+  assert(INT_FAST64_MAX >= 0x7FFFFFFFFFFFFFFFLL);
+  assert(UINT_FAST64_MAX >= 0xFFFFFFFFFFFFFFFFULL);
+
+  assert(INTPTR_MIN <= - (signed_power_of_2<15>::value - 1));
+  assert(INTPTR_MAX >= unsigned_power_of_2<15>::value - 1);
+  assert(UINTPTR_MAX >= unsigned_power_of_2<16>::value - 1);
+
+  assert(INTMAX_MIN <= -0x7FFFFFFFFFFFFFFFLL);
+  assert(INTMAX_MAX >= 0x7FFFFFFFFFFFFFFFLL);
+  assert(UINTMAX_MAX >= 0xFFFFFFFFFFFFFFFFULL);
+
+  // TODO: testcases for macros {PTRDIFF, SIG_ATOMIC, WCHAR, WINT}_{MIN, MAX} SIZE_MAX
+
+
+
+  /*
+   * assertions for the type of the macros for integer constants
+   */
+
+  assert(typeid(INTMAX_C(0)) == typeid(std::intmax_t));
+  assert(typeid(UINTMAX_C(0)) == typeid(std::uintmax_t));
+
+  return EXIT_SUCCESS;
+}
diff --git a/testcase/cstdlib/quick_exit-exceptions.cpp b/testcase/cstdlib/quick_exit-exceptions.cpp
new file mode 100644
index 0000000..81eb440
--- /dev/null
+++ b/testcase/cstdlib/quick_exit-exceptions.cpp
@@ -0,0 +1,38 @@
+/*
+Permission is granted to use, modify, and / or redistribute at will.
+
+This includes removing authorship notices, re-use of code parts in
+other software (with or without giving credit), and / or creating a
+commercial product based on it.
+
+This permission is not revocable by the author.
+
+This software is provided as-is. Use it at your own risk. There is
+no warranty whatsoever, neither expressed nor implied, and by using
+this software you accept that the author(s) shall not be held liable
+for any loss of data, loss of service, or other damages, be they
+incidental or consequential. Your only option other than accepting
+this is not to use the software at all.
+*/
+#include <cstdlib>
+#include <exception>
+// TODO: #include <cassert>
+#include <assert.h>
+
+/*
+ * Check that the quick_exit function calls std::terminate when a handler
+ * leaks an exception
+ */
+
+void f()
+{
+  throw std::exception();
+}
+
+int main()
+{
+  assert(std::at_quick_exit(f) == 0);
+  std::quick_exit(EXIT_SUCCESS);
+
+  return EXIT_FAILURE;
+}
diff --git a/testcase/cstdlib/quick_exit.cpp b/testcase/cstdlib/quick_exit.cpp
new file mode 100644
index 0000000..efdbdfd
--- /dev/null
+++ b/testcase/cstdlib/quick_exit.cpp
@@ -0,0 +1,52 @@
+/*
+Permission is granted to use, modify, and / or redistribute at will.
+
+This includes removing authorship notices, re-use of code parts in
+other software (with or without giving credit), and / or creating a
+commercial product based on it.
+
+This permission is not revocable by the author.
+
+This software is provided as-is. Use it at your own risk. There is
+no warranty whatsoever, neither expressed nor implied, and by using
+this software you accept that the author(s) shall not be held liable
+for any loss of data, loss of service, or other damages, be they
+incidental or consequential. Your only option other than accepting
+this is not to use the software at all.
+*/
+#include <cstdlib>
+// TODO: include <cassert>
+#include <assert.h>
+
+/*
+ * Check that the quick_exit functions get called in the right order
+ */
+
+static int count = 0;
+
+void e()
+{
+  assert(count == 2);
+  std::_Exit(EXIT_SUCCESS);
+}
+
+void f()
+{
+  assert(count == 1);
+  count = 2;
+}
+
+void g()
+{
+  count = 1;
+  assert(std::at_quick_exit(f) == 0);
+}
+
+int main()
+{
+  assert(std::at_quick_exit(e) == 0);
+  assert(std::at_quick_exit(g) == 0);
+  std::quick_exit(EXIT_FAILURE);
+
+  return EXIT_FAILURE;
+}
diff --git a/testcase/new/bad_alloc.cpp b/testcase/new/bad_alloc.cpp
new file mode 100644
index 0000000..a092525
--- /dev/null
+++ b/testcase/new/bad_alloc.cpp
@@ -0,0 +1,37 @@
+/*
+Permission is granted to use, modify, and / or redistribute at will.
+
+This includes removing authorship notices, re-use of code parts in
+other software (with or without giving credit), and / or creating a
+commercial product based on it.
+
+This permission is not revocable by the author.
+
+This software is provided as-is. Use it at your own risk. There is
+no warranty whatsoever, neither expressed nor implied, and by using
+this software you accept that the author(s) shall not be held liable
+for any loss of data, loss of service, or other damages, be they
+incidental or consequential. Your only option other than accepting
+this is not to use the software at all.
+*/
+#include <new>
+#include <cstdlib>
+
+/*
+ * Checks that new does in fact throw a std::bad_alloc exception, when it is
+ * faced with an unsatisfiable request
+ */
+
+int main()
+{
+  try
+  {
+    new char[static_cast<std::size_t>(-1)];
+  }
+  catch (std::bad_alloc& e)
+  {
+    return EXIT_SUCCESS;
+  }
+
+  return EXIT_FAILURE;
+}
diff --git a/testcase/new/new_handler.cpp b/testcase/new/new_handler.cpp
new file mode 100644
index 0000000..cc451da
--- /dev/null
+++ b/testcase/new/new_handler.cpp
@@ -0,0 +1,37 @@
+/*
+Permission is granted to use, modify, and / or redistribute at will.
+
+This includes removing authorship notices, re-use of code parts in
+other software (with or without giving credit), and / or creating a
+commercial product based on it.
+
+This permission is not revocable by the author.
+
+This software is provided as-is. Use it at your own risk. There is
+no warranty whatsoever, neither expressed nor implied, and by using
+this software you accept that the author(s) shall not be held liable
+for any loss of data, loss of service, or other damages, be they
+incidental or consequential. Your only option other than accepting
+this is not to use the software at all.
+*/
+#include <new>
+#include <cstdlib>
+
+/*
+ * Checks that the new_handler is 0 on startup and that new calls an installed
+ * new_handler when it is faced with an unsatisfiable request
+ */
+
+void my_new_handler()
+{
+  std::exit(EXIT_SUCCESS);
+}
+
+int main()
+{
+  if (std::set_new_handler(my_new_handler) != 0)
+    return EXIT_FAILURE;
+
+  new char[static_cast<std::size_t>(-1)];
+  return EXIT_FAILURE;
+}
diff --git a/testcase/new/nothrow.cpp b/testcase/new/nothrow.cpp
new file mode 100644
index 0000000..ca7d22d
--- /dev/null
+++ b/testcase/new/nothrow.cpp
@@ -0,0 +1,31 @@
+/*
+Permission is granted to use, modify, and / or redistribute at will.
+
+This includes removing authorship notices, re-use of code parts in
+other software (with or without giving credit), and / or creating a
+commercial product based on it.
+
+This permission is not revocable by the author.
+
+This software is provided as-is. Use it at your own risk. There is
+no warranty whatsoever, neither expressed nor implied, and by using
+this software you accept that the author(s) shall not be held liable
+for any loss of data, loss of service, or other damages, be they
+incidental or consequential. Your only option other than accepting
+this is not to use the software at all.
+*/
+#include <new>
+#include <cstdlib>
+
+/*
+ * Checks that the nothrow-new does not throw, but return 0, when it is faced
+ * with an unsatisfiable request
+ */
+
+int main()
+{
+  if (new (std::nothrow) char[static_cast<std::size_t>(-1)] == 0)
+    return EXIT_SUCCESS;
+
+  return EXIT_FAILURE;
+}
-- 
1.6.0.4