[coco] Introduce BlockIndex class (#1655)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Thu, 27 Sep 2018 09:55:06 +0000 (18:55 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Thu, 27 Sep 2018 09:55:06 +0000 (18:55 +0900)
This commit introduces BlockIndex class which denotes the index of a
block inside a block list.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/coco/core/include/coco/IR/BlockIndex.h [new file with mode: 0644]
contrib/coco/core/src/IR/BlockIndex.cpp [new file with mode: 0644]
contrib/coco/core/src/IR/BlockIndex.test.cpp [new file with mode: 0644]

diff --git a/contrib/coco/core/include/coco/IR/BlockIndex.h b/contrib/coco/core/include/coco/IR/BlockIndex.h
new file mode 100644 (file)
index 0000000..2418748
--- /dev/null
@@ -0,0 +1,47 @@
+#ifndef __COCO_IR_BLOCK_INDEX_H__
+#define __COCO_IR_BLOCK_INDEX_H__
+
+#include <cstdint>
+
+namespace coco
+{
+
+/**
+ * @brief A BlockIndex denotes the index of a block in a block list
+ */
+class BlockIndex final
+{
+private:
+  static const uint32_t undefined = 0xffffffff;
+
+public:
+  BlockIndex() : _value{undefined}
+  {
+    // DO NOTHING
+  }
+
+public:
+  BlockIndex(uint32_t value) { set(value); }
+
+public:
+  bool valid(void) const { return _value != undefined; }
+
+public:
+  uint32_t value(void) const { return _value; }
+
+public:
+  void set(uint32_t value);
+  void reset(void) { _value = undefined; }
+
+private:
+  uint32_t _value;
+};
+
+static inline bool operator<(const BlockIndex &lhs, const BlockIndex &rhs)
+{
+  return lhs.value() < rhs.value();
+}
+
+} // namespace coco
+
+#endif // __COCO_IR_BLOCK_INDEX_H__
diff --git a/contrib/coco/core/src/IR/BlockIndex.cpp b/contrib/coco/core/src/IR/BlockIndex.cpp
new file mode 100644 (file)
index 0000000..eb86c6a
--- /dev/null
@@ -0,0 +1,14 @@
+#include "coco/IR/BlockIndex.h"
+
+#include <cassert>
+
+namespace coco
+{
+
+void BlockIndex::set(uint32_t value)
+{
+  assert(value != undefined);
+  _value = value;
+}
+
+} // namespace coco
diff --git a/contrib/coco/core/src/IR/BlockIndex.test.cpp b/contrib/coco/core/src/IR/BlockIndex.test.cpp
new file mode 100644 (file)
index 0000000..a95cf98
--- /dev/null
@@ -0,0 +1,34 @@
+#include "coco/IR/BlockIndex.h"
+
+#include <gtest/gtest.h>
+
+namespace
+{
+
+class BlockIndexTest : public ::testing::Test
+{
+};
+
+} // namespace
+
+TEST_F(BlockIndexTest, default_constructor)
+{
+  coco::BlockIndex blk_ind;
+
+  ASSERT_FALSE(blk_ind.valid());
+}
+
+TEST_F(BlockIndexTest, explicit_constructor)
+{
+  coco::BlockIndex blk_ind{3};
+
+  ASSERT_TRUE(blk_ind.valid());
+  ASSERT_EQ(blk_ind.value(), 3);
+}
+
+TEST_F(BlockIndexTest, operator_lt)
+{
+  // Valid index is always less than undefined one.
+  ASSERT_TRUE(coco::BlockIndex(3) < coco::BlockIndex());
+  ASSERT_TRUE(coco::BlockIndex(3) < coco::BlockIndex(4));
+}