fix some of the warning/errors clang 3.1 reports - 2
[folly.git] / folly / experimental / test / GenTest.cpp
index c362783165e1cdd1a3a4b077cc208aaac522e555..de2e4afbebe63a099daa330afb99e65c64877f32 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2012 Facebook, Inc.
+ * Copyright 2013 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -251,8 +251,8 @@ TEST(Gen, OrderTake) {
 
 TEST(Gen, MinBy) {
   EXPECT_EQ(7, seq(1, 10)
-             | minBy([](int i) {
-                 auto d = i - 6.8;
+             | minBy([](int i) -> double {
+                 double d = i - 6.8;
                  return d * d;
                }));
 }
@@ -403,7 +403,7 @@ TEST(Gen, Any) {
 }
 
 TEST(Gen, Yielders) {
-  auto gen = GENERATOR(int, {
+  auto gen = GENERATOR(int) {
     for (int i = 1; i <= 5; ++i) {
       yield(i);
     }
@@ -411,7 +411,7 @@ TEST(Gen, Yielders) {
     for (int i = 3; ; ++i) {
       yield(i * i);
     }
-  });
+  };
   vector<int> expected {
     1, 2, 3, 4, 5, 7, 9, 16, 25
   };
@@ -419,30 +419,30 @@ TEST(Gen, Yielders) {
 }
 
 TEST(Gen, NestedYield) {
-  auto nums = GENERATOR(int, {
+  auto nums = GENERATOR(int) {
     for (int i = 1; ; ++i) {
       yield(i);
     }
-  });
-  auto gen = GENERATOR(int, {
+  };
+  auto gen = GENERATOR(int) {
     nums | take(10) | yield;
     seq(1, 5) | [&](int i) {
       yield(i);
     };
-  });
+  };
   EXPECT_EQ(70, gen | sum);
 }
 
 TEST(Gen, MapYielders) {
   auto gen = seq(1, 5)
            | map([](int n) {
-               return GENERATOR(int, {
+               return GENERATOR(int) {
                  int i;
                  for (i = 1; i < n; ++i)
                    yield(i);
                  for (; i >= 1; --i)
                    yield(i);
-               });
+               };
              })
            | concat;
   vector<int> expected {
@@ -596,6 +596,65 @@ TEST(Gen, Dynamic) {
 }
 
 TEST(StringGen, EmptySplit) {
+  auto collect = eachTo<std::string>() | as<vector>();
+  {
+    auto pieces = split("", ',') | collect;
+    EXPECT_EQ(0, pieces.size());
+  }
+
+  // The last delimiter is eaten, just like std::getline
+  {
+    auto pieces = split(",", ',') | collect;
+    EXPECT_EQ(1, pieces.size());
+    EXPECT_EQ("", pieces[0]);
+  }
+
+  {
+    auto pieces = split(",,", ',') | collect;
+    EXPECT_EQ(2, pieces.size());
+    EXPECT_EQ("", pieces[0]);
+    EXPECT_EQ("", pieces[1]);
+  }
+
+  {
+    auto pieces = split(",,", ',') | take(1) | collect;
+    EXPECT_EQ(1, pieces.size());
+    EXPECT_EQ("", pieces[0]);
+  }
+}
+
+TEST(StringGen, Split) {
+  auto collect = eachTo<std::string>() | as<vector>();
+  {
+    auto pieces = split("hello,, world, goodbye, meow", ',') | collect;
+    EXPECT_EQ(5, pieces.size());
+    EXPECT_EQ("hello", pieces[0]);
+    EXPECT_EQ("", pieces[1]);
+    EXPECT_EQ(" world", pieces[2]);
+    EXPECT_EQ(" goodbye", pieces[3]);
+    EXPECT_EQ(" meow", pieces[4]);
+  }
+
+  {
+    auto pieces = split("hello,, world, goodbye, meow", ',')
+                | take(3) | collect;
+    EXPECT_EQ(3, pieces.size());
+    EXPECT_EQ("hello", pieces[0]);
+    EXPECT_EQ("", pieces[1]);
+    EXPECT_EQ(" world", pieces[2]);
+  }
+
+  {
+    auto pieces = split("hello,, world, goodbye, meow", ',')
+                | take(5) | collect;
+    EXPECT_EQ(5, pieces.size());
+    EXPECT_EQ("hello", pieces[0]);
+    EXPECT_EQ("", pieces[1]);
+    EXPECT_EQ(" world", pieces[2]);
+  }
+}
+
+TEST(StringGen, EmptyResplit) {
   auto collect = eachTo<std::string>() | as<vector>();
   {
     auto pieces = from({""}) | resplit(',') | collect;
@@ -617,7 +676,7 @@ TEST(StringGen, EmptySplit) {
   }
 }
 
-TEST(StringGen, Split) {
+TEST(StringGen, Resplit) {
   auto collect = eachTo<std::string>() | as<vector>();
   {
     auto pieces = from({"hello,, world, goodbye, meow"}) |