From f6fccc466a891f5464ea56200f2d025f1a223083 Mon Sep 17 00:00:00 2001 From: Yifan Hong Date: Thu, 12 Sep 2019 13:00:54 -0700 Subject: [PATCH] Move digital_stroage.h to its own library ...so that it can be used by other tests. Also, clean up the code before exposing it. Test: libsnapshot_test Test: liblp_test Change-Id: I627326f696ea55b7113ff26b313f7dd04e341dc1 --- fs_mgr/liblp/Android.bp | 3 ++ fs_mgr/liblp/builder_test.cpp | 9 ++---- fs_mgr/libsnapshot/Android.bp | 3 ++ fs_mgr/libsnapshot/snapshot_test.cpp | 6 ++-- fs_mgr/libstorage_literals/Android.bp | 6 ++++ .../storage_literals/storage_literals.h} | 28 +++++++++---------- 6 files changed, 31 insertions(+), 24 deletions(-) create mode 100644 fs_mgr/libstorage_literals/Android.bp rename fs_mgr/{libsnapshot/digital_storage.h => libstorage_literals/storage_literals/storage_literals.h} (72%) diff --git a/fs_mgr/liblp/Android.bp b/fs_mgr/liblp/Android.bp index f0142bbab..b2572f64d 100644 --- a/fs_mgr/liblp/Android.bp +++ b/fs_mgr/liblp/Android.bp @@ -67,6 +67,9 @@ cc_defaults { "libfs_mgr", "liblp", ] + liblp_lib_deps, + header_libs: [ + "libstorage_literals_headers", + ], stl: "libc++_static", srcs: [ "builder_test.cpp", diff --git a/fs_mgr/liblp/builder_test.cpp b/fs_mgr/liblp/builder_test.cpp index bd41f592c..a67ffa782 100644 --- a/fs_mgr/liblp/builder_test.cpp +++ b/fs_mgr/liblp/builder_test.cpp @@ -17,11 +17,13 @@ #include #include #include +#include #include "liblp_test.h" #include "utility.h" using namespace std; +using namespace android::storage_literals; using namespace android::fs_mgr; using namespace android::fs_mgr::testing; using ::testing::_; @@ -591,13 +593,6 @@ TEST_F(BuilderTest, ChangeGroups) { ASSERT_NE(builder->Export(), nullptr); } -constexpr unsigned long long operator"" _GiB(unsigned long long x) { // NOLINT - return x << 30; -} -constexpr unsigned long long operator"" _MiB(unsigned long long x) { // NOLINT - return x << 20; -} - TEST_F(BuilderTest, RemoveAndAddFirstPartition) { auto builder = MetadataBuilder::New(10_GiB, 65536, 2); ASSERT_NE(nullptr, builder); diff --git a/fs_mgr/libsnapshot/Android.bp b/fs_mgr/libsnapshot/Android.bp index f2b6141b8..f73b18975 100644 --- a/fs_mgr/libsnapshot/Android.bp +++ b/fs_mgr/libsnapshot/Android.bp @@ -103,4 +103,7 @@ cc_test { "libsparse", "libz", ], + header_libs: [ + "libstorage_literals_headers", + ], } diff --git a/fs_mgr/libsnapshot/snapshot_test.cpp b/fs_mgr/libsnapshot/snapshot_test.cpp index 876b8f897..e9835d06d 100644 --- a/fs_mgr/libsnapshot/snapshot_test.cpp +++ b/fs_mgr/libsnapshot/snapshot_test.cpp @@ -32,8 +32,8 @@ #include #include #include +#include -#include "digital_storage.h" #include "test_helpers.h" #include "utility.h" @@ -52,7 +52,7 @@ using android::fs_mgr::GetPartitionName; using android::fs_mgr::MetadataBuilder; using namespace ::testing; using namespace android::fs_mgr::testing; -using namespace android::digital_storage; +using namespace android::storage_literals; using namespace std::chrono_literals; using namespace std::string_literals; @@ -62,7 +62,7 @@ std::unique_ptr sm; TestDeviceInfo* test_device = nullptr; std::string fake_super; -static constexpr uint64_t kSuperSize = (16_MiB).bytes(); +static constexpr uint64_t kSuperSize = 16_MiB; class SnapshotTest : public ::testing::Test { public: diff --git a/fs_mgr/libstorage_literals/Android.bp b/fs_mgr/libstorage_literals/Android.bp new file mode 100644 index 000000000..11611dd9e --- /dev/null +++ b/fs_mgr/libstorage_literals/Android.bp @@ -0,0 +1,6 @@ + +cc_library_headers { + name: "libstorage_literals_headers", + host_supported: true, + export_include_dirs: ["."], +} diff --git a/fs_mgr/libsnapshot/digital_storage.h b/fs_mgr/libstorage_literals/storage_literals/storage_literals.h similarity index 72% rename from fs_mgr/libsnapshot/digital_storage.h rename to fs_mgr/libstorage_literals/storage_literals/storage_literals.h index 210298ea2..ac0dfbdb7 100644 --- a/fs_mgr/libsnapshot/digital_storage.h +++ b/fs_mgr/libstorage_literals/storage_literals/storage_literals.h @@ -18,25 +18,25 @@ #include namespace android { -namespace digital_storage { +namespace storage_literals { template struct Size { static constexpr size_t power = Power; - constexpr Size(uint64_t count) : value_(count) {} + explicit constexpr Size(uint64_t count) : value_(count) {} - constexpr uint64_t bytes() const { return value_ << (Power * 10); } + constexpr uint64_t bytes() const { return value_ << power; } constexpr uint64_t count() const { return value_; } - operator uint64_t() const { return bytes(); } + constexpr operator uint64_t() const { return bytes(); } private: uint64_t value_; }; using B = Size<0>; -using KiB = Size<1>; -using MiB = Size<2>; -using GiB = Size<3>; +using KiB = Size<10>; +using MiB = Size<20>; +using GiB = Size<30>; constexpr B operator""_B(unsigned long long v) { // NOLINT return B{v}; @@ -57,21 +57,21 @@ constexpr GiB operator""_GiB(unsigned long long v) { // NOLINT template constexpr Dest size_cast(Src src) { if (Src::power < Dest::power) { - return Dest(src.count() >> ((Dest::power - Src::power) * 10)); + return Dest(src.count() >> (Dest::power - Src::power)); } if (Src::power > Dest::power) { - return Dest(src.count() << ((Src::power - Dest::power) * 10)); + return Dest(src.count() << (Src::power - Dest::power)); } return Dest(src.count()); } -static_assert((1_B).bytes() == 1); -static_assert((1_KiB).bytes() == 1 << 10); -static_assert((1_MiB).bytes() == 1 << 20); -static_assert((1_GiB).bytes() == 1 << 30); +static_assert(1_B == 1); +static_assert(1_KiB == 1 << 10); +static_assert(1_MiB == 1 << 20); +static_assert(1_GiB == 1 << 30); static_assert(size_cast(1_B).count() == 0); static_assert(size_cast(1024_B).count() == 1); static_assert(size_cast(1_MiB).count() == 1024); -} // namespace digital_storage +} // namespace storage_literals } // namespace android