Improve the <sstream> test.

It looks like libcxx's testing is crap too, and this turned up
another bug in stlport.

Change-Id: I98c93dceaa8225c3cc280166d4bb74adaf94a324
diff --git a/tests/sstream_test.cpp b/tests/sstream_test.cpp
index 6b59542..d5f8695 100644
--- a/tests/sstream_test.cpp
+++ b/tests/sstream_test.cpp
@@ -16,15 +16,55 @@
 
 #include <gtest/gtest.h>
 
+#include <stdint.h>
+#include <limits>
 #include <sstream>
 
 // TODO: move this test to libcxx.
 
-TEST(sstream, __get_integer_overflow) {
-  std::stringstream ss("9223372036854775808");
-  int64_t result;
+template <typename T>
+static void CheckOverflow(T expected, const char* value, bool should_overflow) {
+  std::stringstream ss(value);
+  T result = T(0);
   ss >> result;
-  EXPECT_TRUE(ss.eof());
-  EXPECT_TRUE(ss.fail());
-  EXPECT_FALSE(ss.bad());
+  EXPECT_FALSE(ss.bad()) << value << ' ' << int64_t(result);
+  EXPECT_EQ(should_overflow, ss.fail()) << value << ' ' << int64_t(result);
+  if (!should_overflow) { // glibc writes garbage on overflow.
+    ASSERT_EQ(expected, result) << value;
+  }
+}
+
+TEST(sstream, __get_integer_overflow_8) {
+  // The usual byte/char confusion means that operator>> on 8-bit types is used
+  // for chars, so there's no possibility of overflow.
+}
+
+TEST(sstream, __get_integer_overflow_16) {
+  CheckOverflow<int16_t>(std::numeric_limits<int16_t>::min(), "-32768", false);
+  CheckOverflow<int16_t>(0, "-32769", true);
+  CheckOverflow<int16_t>(std::numeric_limits<int16_t>::max(), "32767", false);
+  CheckOverflow<int16_t>(0, "32768", true);
+
+  CheckOverflow<uint16_t>(std::numeric_limits<uint16_t>::max(), "65535", false);
+  CheckOverflow<uint16_t>(0, "65536", true);
+}
+
+TEST(sstream, __get_integer_overflow_32) {
+  CheckOverflow<int32_t>(std::numeric_limits<int32_t>::min(), "-2147483648", false);
+  CheckOverflow<int32_t>(0, "-2147483649", true);
+  CheckOverflow<int32_t>(std::numeric_limits<int32_t>::max(), "2147483647", false);
+  CheckOverflow<int32_t>(0, "2147483648", true);
+
+  CheckOverflow<uint32_t>(std::numeric_limits<uint32_t>::max(), "4294967295", false);
+  CheckOverflow<uint32_t>(0, "4294967296", true);
+}
+
+TEST(sstream, __get_integer_overflow_64) {
+  CheckOverflow<int64_t>(std::numeric_limits<int64_t>::min(), "-9223372036854775808", false);
+  CheckOverflow<int64_t>(0, "-9223372036854775809", true);
+  CheckOverflow<int64_t>(std::numeric_limits<int64_t>::max(), "9223372036854775807", false);
+  CheckOverflow<int64_t>(0, "9223372036854775808", true);
+
+  CheckOverflow<uint64_t>(std::numeric_limits<uint64_t>::max(), "18446744073709551615", false);
+  CheckOverflow<uint64_t>(0, "18446744073709551616", true);
 }