Bug 719531 - Part 1: Add test that FallibleTArray returns false instead of crashing. r=bsmedberg
authorJustin Lebar <justin.lebar@gmail.com>
Thu, 26 Jan 2012 12:51:34 -0500
changeset 86719 e8c23cd5fc7985ca235e97aba11f3282bc14c51a
parent 86718 fccccb47213ed5351a957a6fce612cf3864803e5
child 86720 1d68a3277ada46ff436cb0496b69bb3d4b2e8e04
push id805
push userakeybl@mozilla.com
push dateWed, 01 Feb 2012 18:17:35 +0000
treeherdermozilla-aurora@6fb3bf232436 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbsmedberg
bugs719531
milestone12.0a1
Bug 719531 - Part 1: Add test that FallibleTArray returns false instead of crashing. r=bsmedberg
xpcom/tests/TestTArray.cpp
--- a/xpcom/tests/TestTArray.cpp
+++ b/xpcom/tests/TestTArray.cpp
@@ -860,16 +860,52 @@ static bool test_swap() {
     CHECK_NOT_USING_AUTO(b);
     CHECK_ARRAY(b, data1);
     CHECK_EQ_INT(a.Length(), 0);
   }
 
   return true;
 }
 
+static bool test_fallible()
+{
+  // Test that FallibleTArray works properly; that is, it never OOMs, but
+  // instead eventually returns false.
+  //
+  // This test is only meaningful on 32-bit systems.  On a 64-bit system, we
+  // might never OOM.
+  if (sizeof(void*) > 4) {
+    return true;
+  }
+
+  // Allocate a bunch of 512MB arrays.  We could go bigger, but nsTArray will
+  // bail before even attempting to malloc() for gigantic arrays.  512MB should
+  // be under that threshold.
+  //
+  // 9 * 512MB > 4GB, so we should definitely OOM by the 9th array.
+  const int numArrays = 9;
+  FallibleTArray<char> arrays[numArrays];
+  for (PRUint32 i = 0; i < numArrays; i++) {
+    bool success = arrays[i].SetCapacity(512 * 1024 * 1024);
+    if (!success) {
+      // We got our OOM.  Check that it didn't come too early.
+      if (i < 2) {
+        printf("test_fallible: Got OOM on iteration %d.  Too early!\n", i);
+        return false;
+      }
+      return true;
+    }
+  }
+
+  // No OOM?  That's...weird.
+  printf("test_fallible: Didn't OOM or crash?  nsTArray::SetCapacity "
+         "must be lying.\n");
+  return false;
+}
+
 //----
 
 typedef bool (*TestFunc)();
 #define DECL_TEST(name) { #name, name }
 
 static const struct Test {
   const char* name;
   TestFunc    func;
@@ -884,16 +920,17 @@ static const struct Test {
   DECL_TEST(test_refptr_array),
   DECL_TEST(test_ptrarray),
 #ifdef DEBUG
   DECL_TEST(test_autoarray),
 #endif
   DECL_TEST(test_indexof),
   DECL_TEST(test_heap),
   DECL_TEST(test_swap),
+  DECL_TEST(test_fallible),
   { nsnull, nsnull }
 };
 
 }
 
 using namespace TestTArray;
 
 int main(int argc, char **argv) {