[yaml2obj/obj2yaml] - Add support for .stack_sizes sections.
[llvm-complete.git] / unittests / Support / CachePruningTest.cpp
blobeeffe218ae6cbeeb11d3c2ed0827b9179f5f1621
1 //===- CachePruningTest.cpp -----------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/CachePruning.h"
10 #include "llvm/Support/Error.h"
11 #include "gtest/gtest.h"
13 using namespace llvm;
15 TEST(CachePruningPolicyParser, Empty) {
16 auto P = parseCachePruningPolicy("");
17 ASSERT_TRUE(bool(P));
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");
25 ASSERT_TRUE(bool(P));
26 EXPECT_EQ(std::chrono::seconds(1), P->Interval);
27 P = parseCachePruningPolicy("prune_interval=2m");
28 ASSERT_TRUE(bool(P));
29 EXPECT_EQ(std::chrono::minutes(2), *P->Interval);
30 P = parseCachePruningPolicy("prune_interval=3h");
31 ASSERT_TRUE(bool(P));
32 EXPECT_EQ(std::chrono::hours(3), *P->Interval);
35 TEST(CachePruningPolicyParser, Expiration) {
36 auto P = parseCachePruningPolicy("prune_after=1s");
37 ASSERT_TRUE(bool(P));
38 EXPECT_EQ(std::chrono::seconds(1), P->Expiration);
41 TEST(CachePruningPolicyParser, MaxSizePercentageOfAvailableSpace) {
42 auto P = parseCachePruningPolicy("cache_size=100%");
43 ASSERT_TRUE(bool(P));
44 EXPECT_EQ(100u, P->MaxSizePercentageOfAvailableSpace);
45 EXPECT_EQ(0u, P->MaxSizeBytes);
48 TEST(CachePruningPolicyParser, MaxSizeBytes) {
49 auto P = parseCachePruningPolicy("cache_size_bytes=1");
50 ASSERT_TRUE(bool(P));
51 EXPECT_EQ(75u, P->MaxSizePercentageOfAvailableSpace);
52 EXPECT_EQ(1u, P->MaxSizeBytes);
53 P = parseCachePruningPolicy("cache_size_bytes=2k");
54 ASSERT_TRUE(bool(P));
55 EXPECT_EQ(75u, P->MaxSizePercentageOfAvailableSpace);
56 EXPECT_EQ(2u * 1024u, P->MaxSizeBytes);
57 P = parseCachePruningPolicy("cache_size_bytes=3m");
58 ASSERT_TRUE(bool(P));
59 EXPECT_EQ(75u, P->MaxSizePercentageOfAvailableSpace);
60 EXPECT_EQ(3u * 1024u * 1024u, P->MaxSizeBytes);
61 P = parseCachePruningPolicy("cache_size_bytes=4G");
62 ASSERT_TRUE(bool(P));
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%");
69 ASSERT_TRUE(bool(P));
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()));
88 EXPECT_EQ(
89 "'foo' not an integer",
90 toString(parseCachePruningPolicy("cache_size_bytes=foo").takeError()));
91 EXPECT_EQ(
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()));