From 9551da5c286227535e17d0a20eda71c9f8297163 Mon Sep 17 00:00:00 2001
From: Danilo Piparo <danilo.piparo@cern.ch>
Date: Tue, 3 Oct 2017 13:30:55 +0200
Subject: [PATCH] [TDF] Add tests to stress the caching of C-style arrays

---
 .../test/dataframe/dataframe_cache.cxx        | 87 +++++++++++++++++++
 1 file changed, 87 insertions(+)

diff --git a/tree/treeplayer/test/dataframe/dataframe_cache.cxx b/tree/treeplayer/test/dataframe/dataframe_cache.cxx
index fc402c0519c..56204f31f41 100644
--- a/tree/treeplayer/test/dataframe/dataframe_cache.cxx
+++ b/tree/treeplayer/test/dataframe/dataframe_cache.cxx
@@ -126,6 +126,34 @@ TEST(Cache, InternalColumnsSnapshot)
    EXPECT_EQ(0, ret) << "Internal column " << colName << " has been snapshotted!";
 }
 
+TEST(Cache, CollectionColumns)
+{
+   TDataFrame tdf(3);
+   int i = 0;
+   auto d = tdf.Define("vector", [&i](){std::vector<int> v(3); v[0] = i; v[1] = i+1; v[2] = i+2; return v;})
+               .Define("list", [&i](){std::list<int> v; for (auto j : {0,1,2}) v.emplace_back(j+i); return v;})
+               .Define("deque", [&i](){std::deque<int> v(3); v[0] = i; v[1] = i+1; v[2] = i+2; return v;})
+               .Define("blob", [&i](){return ++i;});
+   {
+      auto c = d.Cache<std::vector<int>, std::list<int>, std::deque<int>>({"vector", "list", "deque"});
+      auto hv = c.Histo1D("vector");
+      auto hl = c.Histo1D("list");
+      auto hd = c.Histo1D("deque");
+      EXPECT_EQ(1, hv->GetMean());
+      EXPECT_EQ(1, hl->GetMean());
+      EXPECT_EQ(1, hd->GetMean());
+   }
+
+   // same but jitted
+   auto c = d.Cache({"vector", "list", "deque"});
+   auto hv = c.Histo1D("vector");
+   auto hl = c.Histo1D("list");
+   auto hd = c.Histo1D("deque");
+   EXPECT_EQ(1, hv->GetMean());
+   EXPECT_EQ(1, hl->GetMean());
+   EXPECT_EQ(1, hd->GetMean());
+}
+
 TEST(Cache, Regex)
 {
 
@@ -163,3 +191,62 @@ TEST(Cache, NonCopiable)
    EXPECT_EQ(0, ret)
       << "The static assert was not triggered even if caching of columns of a non copiable type was requested";
 }
+
+auto treeName = "t";
+auto fileName = "fileName.root";
+
+TEST(Cache, TakeCarrays)
+{
+   {
+      TFile f(fileName, "RECREATE");
+      TTree t(treeName, treeName);
+      float arr[4];
+      t.Branch("arr", arr,"arr[4]/F");
+      for (auto i : ROOT::TSeqU(4)) {
+         for (auto j : ROOT::TSeqU(4)) {
+            arr[j] = i + j;
+         }
+         t.Fill();
+      }
+      t.Write();
+   }
+
+   TDataFrame tdf(treeName, fileName);
+   // no auto here: we check that the type is a COLL<vector<float>>!
+   using ColType_t = std::array_view<float>;
+   std::vector<std::vector<float>> v = *tdf.Take<ColType_t>("arr");
+   std::deque<std::vector<float>> d = *tdf.Take<ColType_t, std::deque<ColType_t>>("arr");
+   std::list<std::vector<float>> l = *tdf.Take<ColType_t, std::list<ColType_t>>("arr");
+
+   auto lit = l.begin();
+   auto ifloat = 0.f;
+   for (auto i : ROOT::TSeqU(4)) {
+      const auto &vv = v[i];
+      const auto &dv = d[i];
+      const auto &lv = *lit;
+      for (auto j : ROOT::TSeqU(4)) {
+         const auto ref = ifloat + j;
+         EXPECT_EQ(ref, vv[j]);
+         EXPECT_EQ(ref, dv[j]);
+         EXPECT_EQ(ref, lv[j]);
+      }
+      ifloat++;
+      lit++;
+   }
+}
+
+TEST(Cache, Carrays)
+{
+   TDataFrame tdf(treeName, fileName);
+   auto cache = tdf.Cache<std::array_view<float>>({"arr"});
+   int i = 0;
+   auto checkArr = [&i](std::vector<float> av){
+      auto ifloat = float(i);
+      EXPECT_EQ(ifloat, av[0]);
+      EXPECT_EQ(ifloat + 1 , av[1]);
+      EXPECT_EQ(ifloat + 2 , av[2]);
+      EXPECT_EQ(ifloat + 3 , av[3]);
+      i++;
+   };
+   cache.Foreach(checkArr,{"arr"});
+}
-- 
GitLab