"Fossies" - the Fresh Open Source Software Archive

Member "apt-1.9.4/test/libapt/srvrecs_test.cc" (19 Sep 2019, 2740 Bytes) of package /linux/misc/apt-1.9.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #include <config.h>
    2 
    3 #include <apt-pkg/srvrec.h>
    4 #include <apt-pkg/strutl.h>
    5 
    6 #include <algorithm>
    7 #include <iostream>
    8 #include <string>
    9 
   10 #include <gtest/gtest.h>
   11 
   12 TEST(SrvRecTest, PopFromSrvRecs)
   13 {
   14    std::vector<SrvRec> Meep;
   15    Meep.emplace_back("foo", 20, 0, 80);
   16    Meep.emplace_back("bar", 20, 0, 80);
   17    Meep.emplace_back("baz", 30, 0, 80);
   18 
   19    EXPECT_EQ(Meep.size(), 3u);
   20    SrvRec const result = PopFromSrvRecs(Meep);
   21    // ensure that pop removed one element
   22    EXPECT_EQ(Meep.size(), 2u);
   23    EXPECT_NE(result.target, "baz");
   24 
   25    SrvRec const result2 = PopFromSrvRecs(Meep);
   26    EXPECT_NE(result.target, result2.target);
   27    EXPECT_NE(result2.target, "baz");
   28    EXPECT_EQ(Meep.size(), 1u);
   29 
   30    SrvRec const result3 = PopFromSrvRecs(Meep);
   31    EXPECT_EQ(result3.target, "baz");
   32    EXPECT_TRUE(Meep.empty());
   33 }
   34 
   35 TEST(SrvRecTest,Randomness)
   36 {
   37    constexpr unsigned int testLength = 100;
   38    std::vector<SrvRec> base1;
   39    std::vector<SrvRec> base2;
   40    std::vector<SrvRec> base3;
   41    for (unsigned int i = 0; i < testLength; ++i)
   42    {
   43       std::string name;
   44       strprintf(name, "foo%d", i);
   45       base1.emplace_back(name, 20, 0, 80);
   46       base2.emplace_back(name, 20, 0, 80);
   47       base3.emplace_back(name, 30, 0, 80);
   48    }
   49    EXPECT_EQ(testLength, base1.size());
   50    EXPECT_EQ(testLength, base2.size());
   51    EXPECT_EQ(testLength, base3.size());
   52    std::move(base3.begin(), base3.end(), std::back_inserter(base2));
   53    EXPECT_EQ(testLength*2, base2.size());
   54 
   55    std::vector<SrvRec> first_pull;
   56    auto const startingClock = clock();
   57    for (unsigned int i = 0; i < testLength; ++i)
   58       first_pull.push_back(PopFromSrvRecs(base1));
   59    EXPECT_TRUE(base1.empty());
   60    EXPECT_FALSE(first_pull.empty());
   61    EXPECT_EQ(testLength, first_pull.size());
   62 
   63    // busy-wait for a cpu-clock change as we use it as "random" value
   64    if (startingClock != -1)
   65       for (int i = 0; i < 100000; ++i)
   66      if (startingClock != clock())
   67         break;
   68 
   69    std::vector<SrvRec> second_pull;
   70    for (unsigned int i = 0; i < testLength; ++i)
   71       second_pull.push_back(PopFromSrvRecs(base2));
   72    EXPECT_FALSE(base2.empty());
   73    EXPECT_FALSE(second_pull.empty());
   74    EXPECT_EQ(testLength, second_pull.size());
   75 
   76    EXPECT_EQ(first_pull.size(), second_pull.size());
   77    EXPECT_TRUE(std::all_of(first_pull.begin(), first_pull.end(), [](SrvRec const &R) { return R.priority == 20; }));
   78    EXPECT_TRUE(std::all_of(second_pull.begin(), second_pull.end(), [](SrvRec const &R) { return R.priority == 20; }));
   79    if (startingClock != -1 && startingClock != clock())
   80    {
   81       EXPECT_FALSE(std::equal(first_pull.begin(), first_pull.end(), second_pull.begin()));
   82    }
   83 
   84    EXPECT_TRUE(std::all_of(base2.begin(), base2.end(), [](SrvRec const &R) { return R.priority == 30; }));
   85 }