1 //===- CachePruningTest.cpp -----------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/CachePruning.h"
11 #include "llvm/Support/Error.h"
12 #include "gtest/gtest.h"
16 TEST(CachePruningPolicyParser
, Empty
) {
17 auto P
= parseCachePruningPolicy("");
19 EXPECT_EQ(std::chrono::seconds(1200), P
->Interval
);
20 EXPECT_EQ(std::chrono::hours(7 * 24), P
->Expiration
);
21 EXPECT_EQ(75u, P
->MaxSizePercentageOfAvailableSpace
);
24 TEST(CachePruningPolicyParser
, Interval
) {
25 auto P
= parseCachePruningPolicy("prune_interval=1s");
27 EXPECT_EQ(std::chrono::seconds(1), P
->Interval
);
28 P
= parseCachePruningPolicy("prune_interval=2m");
30 EXPECT_EQ(std::chrono::minutes(2), *P
->Interval
);
31 P
= parseCachePruningPolicy("prune_interval=3h");
33 EXPECT_EQ(std::chrono::hours(3), *P
->Interval
);
36 TEST(CachePruningPolicyParser
, Expiration
) {
37 auto P
= parseCachePruningPolicy("prune_after=1s");
39 EXPECT_EQ(std::chrono::seconds(1), P
->Expiration
);
42 TEST(CachePruningPolicyParser
, MaxSizePercentageOfAvailableSpace
) {
43 auto P
= parseCachePruningPolicy("cache_size=100%");
45 EXPECT_EQ(100u, P
->MaxSizePercentageOfAvailableSpace
);
46 EXPECT_EQ(0u, P
->MaxSizeBytes
);
49 TEST(CachePruningPolicyParser
, MaxSizeBytes
) {
50 auto P
= parseCachePruningPolicy("cache_size_bytes=1");
52 EXPECT_EQ(75u, P
->MaxSizePercentageOfAvailableSpace
);
53 EXPECT_EQ(1u, P
->MaxSizeBytes
);
54 P
= parseCachePruningPolicy("cache_size_bytes=2k");
56 EXPECT_EQ(75u, P
->MaxSizePercentageOfAvailableSpace
);
57 EXPECT_EQ(2u * 1024u, P
->MaxSizeBytes
);
58 P
= parseCachePruningPolicy("cache_size_bytes=3m");
60 EXPECT_EQ(75u, P
->MaxSizePercentageOfAvailableSpace
);
61 EXPECT_EQ(3u * 1024u * 1024u, P
->MaxSizeBytes
);
62 P
= parseCachePruningPolicy("cache_size_bytes=4G");
64 EXPECT_EQ(75u, P
->MaxSizePercentageOfAvailableSpace
);
65 EXPECT_EQ(4ull * 1024ull * 1024ull * 1024ull, P
->MaxSizeBytes
);
68 TEST(CachePruningPolicyParser
, Multiple
) {
69 auto P
= parseCachePruningPolicy("prune_after=1s:cache_size=50%");
71 EXPECT_EQ(std::chrono::seconds(1200), P
->Interval
);
72 EXPECT_EQ(std::chrono::seconds(1), P
->Expiration
);
73 EXPECT_EQ(50u, P
->MaxSizePercentageOfAvailableSpace
);
76 TEST(CachePruningPolicyParser
, Errors
) {
77 EXPECT_EQ("Duration must not be empty",
78 toString(parseCachePruningPolicy("prune_interval=").takeError()));
79 EXPECT_EQ("'foo' not an integer",
80 toString(parseCachePruningPolicy("prune_interval=foos").takeError()));
81 EXPECT_EQ("'24x' must end with one of 's', 'm' or 'h'",
82 toString(parseCachePruningPolicy("prune_interval=24x").takeError()));
83 EXPECT_EQ("'foo' must be a percentage",
84 toString(parseCachePruningPolicy("cache_size=foo").takeError()));
85 EXPECT_EQ("'foo' not an integer",
86 toString(parseCachePruningPolicy("cache_size=foo%").takeError()));
87 EXPECT_EQ("'101' must be between 0 and 100",
88 toString(parseCachePruningPolicy("cache_size=101%").takeError()));
90 "'foo' not an integer",
91 toString(parseCachePruningPolicy("cache_size_bytes=foo").takeError()));
93 "'foo' not an integer",
94 toString(parseCachePruningPolicy("cache_size_bytes=foom").takeError()));
95 EXPECT_EQ("Unknown key: 'foo'",
96 toString(parseCachePruningPolicy("foo=bar").takeError()));