1 //===- CachePruningTest.cpp -----------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/CachePruning.h"
10 #include "llvm/Support/Error.h"
11 #include "gtest/gtest.h"
15 TEST(CachePruningPolicyParser
, Empty
) {
16 auto P
= parseCachePruningPolicy("");
18 EXPECT_EQ(std::chrono::seconds(1200), P
->Interval
);
19 EXPECT_EQ(std::chrono::hours(7 * 24), P
->Expiration
);
20 EXPECT_EQ(75u, P
->MaxSizePercentageOfAvailableSpace
);
23 TEST(CachePruningPolicyParser
, Interval
) {
24 auto P
= parseCachePruningPolicy("prune_interval=1s");
26 EXPECT_EQ(std::chrono::seconds(1), P
->Interval
);
27 P
= parseCachePruningPolicy("prune_interval=2m");
29 EXPECT_EQ(std::chrono::minutes(2), *P
->Interval
);
30 P
= parseCachePruningPolicy("prune_interval=3h");
32 EXPECT_EQ(std::chrono::hours(3), *P
->Interval
);
35 TEST(CachePruningPolicyParser
, Expiration
) {
36 auto P
= parseCachePruningPolicy("prune_after=1s");
38 EXPECT_EQ(std::chrono::seconds(1), P
->Expiration
);
41 TEST(CachePruningPolicyParser
, MaxSizePercentageOfAvailableSpace
) {
42 auto P
= parseCachePruningPolicy("cache_size=100%");
44 EXPECT_EQ(100u, P
->MaxSizePercentageOfAvailableSpace
);
45 EXPECT_EQ(0u, P
->MaxSizeBytes
);
48 TEST(CachePruningPolicyParser
, MaxSizeBytes
) {
49 auto P
= parseCachePruningPolicy("cache_size_bytes=1");
51 EXPECT_EQ(75u, P
->MaxSizePercentageOfAvailableSpace
);
52 EXPECT_EQ(1u, P
->MaxSizeBytes
);
53 P
= parseCachePruningPolicy("cache_size_bytes=2k");
55 EXPECT_EQ(75u, P
->MaxSizePercentageOfAvailableSpace
);
56 EXPECT_EQ(2u * 1024u, P
->MaxSizeBytes
);
57 P
= parseCachePruningPolicy("cache_size_bytes=3m");
59 EXPECT_EQ(75u, P
->MaxSizePercentageOfAvailableSpace
);
60 EXPECT_EQ(3u * 1024u * 1024u, P
->MaxSizeBytes
);
61 P
= parseCachePruningPolicy("cache_size_bytes=4G");
63 EXPECT_EQ(75u, P
->MaxSizePercentageOfAvailableSpace
);
64 EXPECT_EQ(4ull * 1024ull * 1024ull * 1024ull, P
->MaxSizeBytes
);
67 TEST(CachePruningPolicyParser
, Multiple
) {
68 auto P
= parseCachePruningPolicy("prune_after=1s:cache_size=50%");
70 EXPECT_EQ(std::chrono::seconds(1200), P
->Interval
);
71 EXPECT_EQ(std::chrono::seconds(1), P
->Expiration
);
72 EXPECT_EQ(50u, P
->MaxSizePercentageOfAvailableSpace
);
75 TEST(CachePruningPolicyParser
, Errors
) {
76 EXPECT_EQ("Duration must not be empty",
77 toString(parseCachePruningPolicy("prune_interval=").takeError()));
78 EXPECT_EQ("'foo' not an integer",
79 toString(parseCachePruningPolicy("prune_interval=foos").takeError()));
80 EXPECT_EQ("'24x' must end with one of 's', 'm' or 'h'",
81 toString(parseCachePruningPolicy("prune_interval=24x").takeError()));
82 EXPECT_EQ("'foo' must be a percentage",
83 toString(parseCachePruningPolicy("cache_size=foo").takeError()));
84 EXPECT_EQ("'foo' not an integer",
85 toString(parseCachePruningPolicy("cache_size=foo%").takeError()));
86 EXPECT_EQ("'101' must be between 0 and 100",
87 toString(parseCachePruningPolicy("cache_size=101%").takeError()));
89 "'foo' not an integer",
90 toString(parseCachePruningPolicy("cache_size_bytes=foo").takeError()));
92 "'foo' not an integer",
93 toString(parseCachePruningPolicy("cache_size_bytes=foom").takeError()));
94 EXPECT_EQ("Unknown key: 'foo'",
95 toString(parseCachePruningPolicy("foo=bar").takeError()));