From 7a25e186bb8e949356f0efaba9c5c878c502d445 Mon Sep 17 00:00:00 2001 From: tomershafir Date: Thu, 11 Apr 2024 11:44:54 +0300 Subject: [PATCH] COW: assert in examples --- src/Common/examples/cow_columns.cpp | 68 ++++++++++++++---------- src/Common/examples/cow_compositions.cpp | 60 +++++++++++++-------- 2 files changed, 78 insertions(+), 50 deletions(-) diff --git a/src/Common/examples/cow_columns.cpp b/src/Common/examples/cow_columns.cpp index 404b478f5a0..a0cfc1f12d1 100644 --- a/src/Common/examples/cow_columns.cpp +++ b/src/Common/examples/cow_columns.cpp @@ -1,5 +1,6 @@ #include #include +#include class IColumn : public COW @@ -15,8 +16,6 @@ public: virtual int get() const = 0; virtual void set(int value) = 0; - - virtual MutablePtr test() const = 0; }; using ColumnPtr = IColumn::Ptr; @@ -31,58 +30,69 @@ private: explicit ConcreteColumn(int data_) : data(data_) {} ConcreteColumn(const ConcreteColumn &) = default; - MutableColumnPtr test() const override - { - MutableColumnPtr res = create(123); - return res; - } - public: int get() const override { return data; } void set(int value) override { data = value; } }; +void print(const ColumnPtr & x, const ColumnPtr & y) +{ + std::cerr << "values: " << x->get() << ", " << y->get() << "\n"; + std::cerr << "refcounts: " << x->use_count() << ", " << y->use_count() << "\n"; + std::cerr << "addresses: " << x.get() << ", " << y.get() << "\n"; +} + +void print(const ColumnPtr & x, const MutableColumnPtr & mut) +{ + std::cerr << "values: " << x->get() << ", " << mut->get() << "\n"; + std::cerr << "refcounts: " << x->use_count() << ", " << mut->use_count() << "\n"; + std::cerr << "addresses: " << x.get() << ", " << mut.get() << "\n"; +} int main(int, char **) { ColumnPtr x = ConcreteColumn::create(1); - ColumnPtr y = x;//x->test(); - - std::cerr << "values: " << x->get() << ", " << y->get() << "\n"; - std::cerr << "refcounts: " << x->use_count() << ", " << y->use_count() << "\n"; - std::cerr << "addresses: " << x.get() << ", " << y.get() << "\n"; + ColumnPtr y = x; + print(x, y); + chassert(x->get() == y->get() == 1); + chassert(x->use_count() == y->use_count() == 2); + chassert(x.get() == y.get()); { MutableColumnPtr mut = IColumn::mutate(std::move(y)); mut->set(2); + print(x, mut); + chassert(x->get() == 1 && mut->get() == 2); + chassert(x->use_count() == mut->use_count() == 1); + chassert(x.get() != mut.get()); - std::cerr << "refcounts: " << x->use_count() << ", " << mut->use_count() << "\n"; - std::cerr << "addresses: " << x.get() << ", " << mut.get() << "\n"; y = std::move(mut); } - - std::cerr << "values: " << x->get() << ", " << y->get() << "\n"; - std::cerr << "refcounts: " << x->use_count() << ", " << y->use_count() << "\n"; - std::cerr << "addresses: " << x.get() << ", " << y.get() << "\n"; + print(x, y); + chassert(x->get() == 1 && y->get() == 2); + chassert(x->use_count() == y->use_count() == 1); + chassert(x.get() != y.get()); x = ConcreteColumn::create(0); - - std::cerr << "values: " << x->get() << ", " << y->get() << "\n"; - std::cerr << "refcounts: " << x->use_count() << ", " << y->use_count() << "\n"; - std::cerr << "addresses: " << x.get() << ", " << y.get() << "\n"; + print(x, y); + chassert(x->get() == 0 && y->get() == 2); + chassert(x->use_count() == y->use_count() == 1); + chassert(x.get() != y.get()); { MutableColumnPtr mut = IColumn::mutate(std::move(y)); mut->set(3); + print(x, mut); + chassert(x->get() == 0 && mut->get() == 3); + chassert(x->use_count() == mut->use_count() == 1); + chassert(x.get() != mut.get()); - std::cerr << "refcounts: " << x->use_count() << ", " << mut->use_count() << "\n"; - std::cerr << "addresses: " << x.get() << ", " << mut.get() << "\n"; y = std::move(mut); } - - std::cerr << "values: " << x->get() << ", " << y->get() << "\n"; - std::cerr << "refcounts: " << x->use_count() << ", " << y->use_count() << "\n"; + print(x, y); + chassert(x->get() == 0 && y->get() == 3); + chassert(x->use_count() == y->use_count() == 1); + chassert(x.get() != y.get()); return 0; } - diff --git a/src/Common/examples/cow_compositions.cpp b/src/Common/examples/cow_compositions.cpp index 05d6fe1d89e..3df196bc6cf 100644 --- a/src/Common/examples/cow_compositions.cpp +++ b/src/Common/examples/cow_compositions.cpp @@ -1,5 +1,6 @@ #include #include +#include class IColumn : public COW @@ -61,47 +62,64 @@ public: void set(int value) override { wrapped->set(value); } }; +void print(const ColumnPtr & x, const ColumnPtr & y) +{ + std::cerr << "values: " << x->get() << ", " << y->get() << "\n"; + std::cerr << "refcounts: " << x->use_count() << ", " << y->use_count() << "\n"; + std::cerr << "addresses: " << x.get() << ", " << y.get() << "\n"; +} + +void print(const ColumnPtr & x, const MutableColumnPtr & mut) +{ + std::cerr << "values: " << x->get() << ", " << mut->get() << "\n"; + std::cerr << "refcounts: " << x->use_count() << ", " << mut->use_count() << "\n"; + std::cerr << "addresses: " << x.get() << ", " << mut.get() << "\n"; +} int main(int, char **) { ColumnPtr x = ColumnComposition::create(1); ColumnPtr y = x; - - std::cerr << "values: " << x->get() << ", " << y->get() << "\n"; - std::cerr << "refcounts: " << x->use_count() << ", " << y->use_count() << "\n"; - std::cerr << "addresses: " << x.get() << ", " << y.get() << "\n"; + print(x, y); + chassert(x->get() == y->get() == 1); + chassert(x->use_count() == y->use_count() == 2); + chassert(x.get() == y.get()); { MutableColumnPtr mut = IColumn::mutate(std::move(y)); mut->set(2); + print(x, mut); + chassert(x->get() == 1 && mut->get() == 2); + chassert(x->use_count() == mut->use_count() == 1); + chassert(x.get() != mut.get()); - std::cerr << "refcounts: " << x->use_count() << ", " << mut->use_count() << "\n"; - std::cerr << "addresses: " << x.get() << ", " << mut.get() << "\n"; y = std::move(mut); } - - std::cerr << "values: " << x->get() << ", " << y->get() << "\n"; - std::cerr << "refcounts: " << x->use_count() << ", " << y->use_count() << "\n"; - std::cerr << "addresses: " << x.get() << ", " << y.get() << "\n"; + print(x, y); + chassert(x->get() == 1 && y->get() == 2); + chassert(x->use_count() == y->use_count() == 1); + chassert(x.get() != y.get()); x = ColumnComposition::create(0); - - std::cerr << "values: " << x->get() << ", " << y->get() << "\n"; - std::cerr << "refcounts: " << x->use_count() << ", " << y->use_count() << "\n"; - std::cerr << "addresses: " << x.get() << ", " << y.get() << "\n"; + print(x, y); + chassert(x->get() == 0 && y->get() == 2); + chassert(x->use_count() == y->use_count() == 1); + chassert(x.get() != y.get()); { MutableColumnPtr mut = IColumn::mutate(std::move(y)); mut->set(3); + print(x, mut); + chassert(x->get() == 0 && mut->get() == 3); + chassert(x->use_count() == mut->use_count() == 1); + chassert(x.get() != mut.get()); - std::cerr << "refcounts: " << x->use_count() << ", " << mut->use_count() << "\n"; - std::cerr << "addresses: " << x.get() << ", " << mut.get() << "\n"; y = std::move(mut); } - - std::cerr << "values: " << x->get() << ", " << y->get() << "\n"; - std::cerr << "refcounts: " << x->use_count() << ", " << y->use_count() << "\n"; - + print(x, y); + chassert(x->get() == 0 && y->get() == 3); + chassert(x->use_count() == y->use_count() == 1); + chassert(x.get() != y.get()); + return 0; } -