Support gtest sharding options.

Add new unit tests to cover the new options.

Some updates to the unit tests.

This is slightly different from the normal gtest output. The difference
is that isolated framework prints the total number of DISABLED tests,
where gtest only lists the disabled tests it would have had in the shard.

Bug: 126376458

Test: Ran new unit tests.
Change-Id: Ib95139353302e1b3416f5f03644f01add40db099
diff --git a/Isolate.cpp b/Isolate.cpp
index e70f1c7..7782042 100644
--- a/Isolate.cpp
+++ b/Isolate.cpp
@@ -98,6 +98,13 @@
     PLOG(FATAL) << "Unexpected failure from popen";
   }
 
+  size_t total_shards = options_.total_shards();
+  bool sharded = total_shards > 1;
+  size_t test_count = 0;
+  if (sharded) {
+    test_count = options_.shard_index() + 1;
+  }
+
   bool skip_until_next_suite = false;
   std::string suite_name;
   char* buffer = nullptr;
@@ -133,13 +140,18 @@
           test_name.resize(test_name.size() - 1);
         }
         if (options_.allow_disabled_tests() || !android::base::StartsWith(test_name, "DISABLED_")) {
-          tests_.push_back(std::make_tuple(suite_name, test_name));
-          total_tests_++;
-          if (new_suite) {
-            // Only increment the number of suites when we find at least one test
-            // for the suites.
-            total_suites_++;
-            new_suite = false;
+          if (!sharded || --test_count == 0) {
+            tests_.push_back(std::make_tuple(suite_name, test_name));
+            total_tests_++;
+            if (new_suite) {
+              // Only increment the number of suites when we find at least one test
+              // for the suites.
+              total_suites_++;
+              new_suite = false;
+            }
+            if (sharded) {
+              test_count = total_shards;
+            }
           }
         } else {
           total_disable_tests_++;
@@ -697,6 +709,29 @@
   slow_threshold_ns_ = options_.slow_threshold_ms() * kNsPerMs;
   deadline_threshold_ns_ = options_.deadline_threshold_ms() * kNsPerMs;
 
+  bool sharding_enabled = options_.total_shards() > 1;
+  if (sharding_enabled &&
+      (options_.shard_index() < 0 || options_.shard_index() >= options_.total_shards())) {
+    ColoredPrintf(COLOR_RED,
+                  "Invalid environment variables: we require 0 <= GTEST_SHARD_INDEX < "
+                  "GTEST_TOTAL_SHARDS, but you have GTEST_SHARD_INDEX=%" PRId64
+                  ", GTEST_TOTAL_SHARDS=%" PRId64,
+                  options_.shard_index(), options_.total_shards());
+    printf("\n");
+    return 1;
+  }
+
+  if (!options_.filter().empty()) {
+    ColoredPrintf(COLOR_YELLOW, "Note: Google Test filter = %s", options_.filter().c_str());
+    printf("\n");
+  }
+
+  if (sharding_enabled) {
+    ColoredPrintf(COLOR_YELLOW, "Note: This is test shard %" PRId64 " of %" PRId64,
+                  options_.shard_index() + 1, options_.total_shards());
+    printf("\n");
+  }
+
   EnumerateTests();
 
   // Stop default result printer to avoid environment setup/teardown information for each test.