A few new bionic tests.

Trivial tests for <alloca.h> and <byteswap.h>, plus slightly improved
test coverage for <inttypes.h> and <stdlib.h>.

Bug: N/A
Test: ran tests
Change-Id: Idac4141ffc760c4f7756332477ce5112950d61a5
Signed-off-by: Aleksandra Tsvetkova <aleksandra.tsvetkova@intel.com>
diff --git a/tests/Android.bp b/tests/Android.bp
index c045c1e..28df084 100644
--- a/tests/Android.bp
+++ b/tests/Android.bp
@@ -51,11 +51,13 @@
     name: "libBionicStandardTests",
     defaults: ["bionic_tests_defaults"],
     srcs: [
+        "alloca_test.cpp",
         "arpa_inet_test.cpp",
         "async_safe_test.cpp",
         "assert_test.cpp",
         "buffer_tests.cpp",
         "bug_26110743_test.cpp",
+        "byteswap_test.cpp",
         "complex_test.cpp",
         "complex_force_long_double_test.cpp",
         "ctype_test.cpp",
diff --git a/tests/alloca_test.cpp b/tests/alloca_test.cpp
new file mode 100644
index 0000000..192157d
--- /dev/null
+++ b/tests/alloca_test.cpp
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <alloca.h>
+
+#include <string.h>
+
+#include <gtest/gtest.h>
+
+TEST(alloca, alloca) {
+  // These days, alloca is usually a builtin, so we can't really assert much.
+  void* p = alloca(1024);
+  ASSERT_NE(nullptr, p);
+  memset(p, 0, 1024);
+}
diff --git a/tests/byteswap_test.cpp b/tests/byteswap_test.cpp
new file mode 100644
index 0000000..a18cfa6
--- /dev/null
+++ b/tests/byteswap_test.cpp
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <byteswap.h>
+
+#include <gtest/gtest.h>
+
+static constexpr uint16_t le16 = 0x1234;
+static constexpr uint32_t le32 = 0x12345678;
+static constexpr uint64_t le64 = 0x123456789abcdef0;
+
+static constexpr uint16_t be16 = 0x3412;
+static constexpr uint32_t be32 = 0x78563412;
+static constexpr uint64_t be64 = 0xf0debc9a78563412;
+
+TEST(byteswap, bswap_16) {
+  EXPECT_EQ(le16, bswap_16(be16));
+  EXPECT_EQ(be16, bswap_16(le16));
+}
+
+TEST(byteswap, bswap_32) {
+  EXPECT_EQ(le32, bswap_32(be32));
+  EXPECT_EQ(be32, bswap_32(le32));
+}
+
+TEST(byteswap, bswap_64) {
+  EXPECT_EQ(le64, bswap_64(be64));
+  EXPECT_EQ(be64, bswap_64(le64));
+}
+
diff --git a/tests/inttypes_test.cpp b/tests/inttypes_test.cpp
index da12ec4..08258ac 100644
--- a/tests/inttypes_test.cpp
+++ b/tests/inttypes_test.cpp
@@ -109,6 +109,18 @@
   EXPECT_EQ(L'x', *end);
 }
 
+TEST(inttypes, strtoimax_dec) {
+  char* p;
+  EXPECT_EQ(-18737357, strtoimax("-18737357foobar12", &p, 10));
+  EXPECT_STREQ("foobar12", p);
+}
+
+TEST(inttypes, strtoimax_hex) {
+  char* p;
+  EXPECT_EQ(-0x18737357f, strtoimax("-18737357foobar12", &p, 16));
+  EXPECT_STREQ("oobar12", p);
+}
+
 TEST(inttypes, strtoimax_EINVAL) {
   errno = 0;
   strtoimax("123", NULL, -1);
@@ -121,6 +133,24 @@
   ASSERT_EQ(EINVAL, errno);
 }
 
+TEST(inttypes, strtoumax_dec) {
+  char* p;
+  EXPECT_EQ(18737357U, strtoumax("18737357foobar12", &p, 10));
+  EXPECT_STREQ("foobar12", p);
+}
+
+TEST(inttypes, strtoumax_hex) {
+  char* p;
+  EXPECT_EQ(0x18737357fU, strtoumax("18737357foobar12", &p, 16));
+  EXPECT_STREQ("oobar12", p);
+}
+
+TEST(inttypes, strtoumax_negative) {
+  char* p;
+  EXPECT_EQ(UINTMAX_MAX - 18737357 + 1, strtoumax("-18737357foobar12", &p, 10));
+  EXPECT_STREQ("foobar12", p);
+}
+
 TEST(inttypes, strtoumax_EINVAL) {
   errno = 0;
   strtoumax("123", NULL, -1);
@@ -158,30 +188,88 @@
 }
 
 TEST(inttypes, div) {
-  div_t r = div(-5, 3);
-  ASSERT_EQ(-1, r.quot);
-  ASSERT_EQ(-2, r.rem);
+  div_t r;
+
+  r = div(5, 3);
+  EXPECT_EQ(1, r.quot);
+  EXPECT_EQ(2, r.rem);
+
+  r = div(5, -3);
+  EXPECT_EQ(-1, r.quot);
+  EXPECT_EQ(2, r.rem);
+
+  r = div(-5, 3);
+  EXPECT_EQ(-1, r.quot);
+  EXPECT_EQ(-2, r.rem);
+
+  r = div(-5, -3);
+  EXPECT_EQ(1, r.quot);
+  EXPECT_EQ(-2, r.rem);
 }
 
 TEST(inttypes, ldiv) {
-  ldiv_t r = ldiv(-5, 3);
-  ASSERT_EQ(-1, r.quot);
-  ASSERT_EQ(-2, r.rem);
+  ldiv_t r;
+
+  r = ldiv(5, 3);
+  EXPECT_EQ(1, r.quot);
+  EXPECT_EQ(2, r.rem);
+
+  r = ldiv(5, -3);
+  EXPECT_EQ(-1, r.quot);
+  EXPECT_EQ(2, r.rem);
+
+  r = ldiv(-5, 3);
+  EXPECT_EQ(-1, r.quot);
+  EXPECT_EQ(-2, r.rem);
+
+  r = ldiv(-5, -3);
+  EXPECT_EQ(1, r.quot);
+  EXPECT_EQ(-2, r.rem);
 }
 
 TEST(inttypes, lldiv) {
-  lldiv_t r = lldiv(-5, 3);
-  ASSERT_EQ(-1, r.quot);
-  ASSERT_EQ(-2, r.rem);
+  lldiv_t r;
+
+  r = lldiv(5, 3);
+  EXPECT_EQ(1, r.quot);
+  EXPECT_EQ(2, r.rem);
+
+  r = lldiv(5, -3);
+  EXPECT_EQ(-1, r.quot);
+  EXPECT_EQ(2, r.rem);
+
+  r = lldiv(-5, 3);
+  EXPECT_EQ(-1, r.quot);
+  EXPECT_EQ(-2, r.rem);
+
+  r = lldiv(-5, -3);
+  EXPECT_EQ(1, r.quot);
+  EXPECT_EQ(-2, r.rem);
 }
 
 TEST(inttypes, imaxdiv) {
-  imaxdiv_t r = imaxdiv(-5, 3);
-  ASSERT_EQ(-1, r.quot);
-  ASSERT_EQ(-2, r.rem);
+  imaxdiv_t r;
+
+  r = imaxdiv(5, 3);
+  EXPECT_EQ(1, r.quot);
+  EXPECT_EQ(2, r.rem);
+
+  r = imaxdiv(5, -3);
+  EXPECT_EQ(-1, r.quot);
+  EXPECT_EQ(2, r.rem);
+
+  r = imaxdiv(-5, 3);
+  EXPECT_EQ(-1, r.quot);
+  EXPECT_EQ(-2, r.rem);
+
+  r = imaxdiv(-5, -3);
+  EXPECT_EQ(1, r.quot);
+  EXPECT_EQ(-2, r.rem);
 }
 
 TEST(inttypes, imaxabs) {
-  ASSERT_EQ(INTMAX_MAX, imaxabs(-INTMAX_MAX));
-  ASSERT_EQ(INTMAX_MAX, imaxabs(INTMAX_MAX));
+  EXPECT_EQ(672423489, imaxabs(672423489));
+  EXPECT_EQ(672423489, imaxabs(-672423489));
+  EXPECT_EQ(INTMAX_MAX, imaxabs(-INTMAX_MAX));
+  EXPECT_EQ(INTMAX_MAX, imaxabs(INTMAX_MAX));
 }
diff --git a/tests/pthread_test.cpp b/tests/pthread_test.cpp
old mode 100755
new mode 100644
diff --git a/tests/stdlib_test.cpp b/tests/stdlib_test.cpp
index 0c40380..ed5767c 100644
--- a/tests/stdlib_test.cpp
+++ b/tests/stdlib_test.cpp
@@ -138,6 +138,47 @@
   EXPECT_EQ(795539493, mrand48());
 }
 
+TEST(stdlib, jrand48_distribution) {
+  const int iterations = 4096;
+  const int pivot_low  = 1536;
+  const int pivot_high = 2560;
+
+  unsigned short xsubi[3];
+  int bits[32] = {};
+
+  for (int iter = 0; iter < iterations; ++iter) {
+    long rand_val = jrand48(xsubi);
+    for (int bit = 0; bit < 32; ++bit) {
+      bits[bit] += (static_cast<unsigned long>(rand_val) >> bit) & 0x01;
+    }
+  }
+
+  // Check that bit probability is uniform
+  for (int bit = 0; bit < 32; ++bit) {
+    EXPECT_TRUE((pivot_low <= bits[bit]) && (bits[bit] <= pivot_high));
+  }
+}
+
+TEST(stdlib, mrand48_distribution) {
+  const int iterations = 4096;
+  const int pivot_low  = 1536;
+  const int pivot_high = 2560;
+
+  int bits[32] = {};
+
+  for (int iter = 0; iter < iterations; ++iter) {
+    long rand_val = mrand48();
+    for (int bit = 0; bit < 32; ++bit) {
+      bits[bit] += (static_cast<unsigned long>(rand_val) >> bit) & 0x01;
+    }
+  }
+
+  // Check that bit probability is uniform
+  for (int bit = 0; bit < 32; ++bit) {
+    EXPECT_TRUE((pivot_low <= bits[bit]) && (bits[bit] <= pivot_high));
+  }
+}
+
 TEST(stdlib, posix_memalign_sweep) {
   void* ptr;