Protocol Buffers - Google's data interchange format (grpc依赖)
https://developers.google.com/protocol-buffers/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
581 lines
17 KiB
581 lines
17 KiB
4 years ago
|
// Copyright (c) 2009-2021, Google LLC
|
||
4 years ago
|
// All rights reserved.
|
||
|
//
|
||
|
// Redistribution and use in source and binary forms, with or without
|
||
|
// modification, are permitted provided that the following conditions are met:
|
||
|
// * Redistributions of source code must retain the above copyright
|
||
|
// notice, this list of conditions and the following disclaimer.
|
||
|
// * Redistributions in binary form must reproduce the above copyright
|
||
|
// notice, this list of conditions and the following disclaimer in the
|
||
|
// documentation and/or other materials provided with the distribution.
|
||
|
// * Neither the name of Google LLC nor the
|
||
|
// names of its contributors may be used to endorse or promote products
|
||
|
// derived from this software without specific prior written permission.
|
||
|
//
|
||
3 years ago
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||
|
// ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
|
||
|
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||
4 years ago
|
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||
|
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||
|
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||
|
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||
|
|
||
13 years ago
|
/*
|
||
|
* Tests for upb_table.
|
||
|
*/
|
||
16 years ago
|
|
||
11 years ago
|
#include <limits.h>
|
||
14 years ago
|
#include <string.h>
|
||
|
#include <sys/resource.h>
|
||
3 years ago
|
|
||
14 years ago
|
#include <iostream>
|
||
16 years ago
|
#include <map>
|
||
14 years ago
|
#include <set>
|
||
16 years ago
|
#include <string>
|
||
6 years ago
|
#include <unordered_map>
|
||
16 years ago
|
#include <vector>
|
||
6 years ago
|
|
||
3 years ago
|
#include "gtest/gtest.h"
|
||
4 years ago
|
#include "upb/table_internal.h"
|
||
3 years ago
|
#include "upb/upb.hpp"
|
||
16 years ago
|
|
||
3 years ago
|
// Must be last.
|
||
6 years ago
|
#include "upb/port_def.inc"
|
||
|
|
||
9 years ago
|
// Convenience interface for C++. We don't put this in upb itself because
|
||
|
// the table is not exposed to users.
|
||
|
|
||
|
namespace upb {
|
||
|
|
||
3 years ago
|
template <class T>
|
||
|
upb_value MakeUpbValue(T val);
|
||
|
template <class T>
|
||
|
T GetUpbValue(upb_value val);
|
||
|
|
||
|
#define FUNCS(name, type_t, enumval) \
|
||
|
template <> \
|
||
|
upb_value MakeUpbValue<type_t>(type_t val) { \
|
||
|
return upb_value_##name(val); \
|
||
|
} \
|
||
|
template <> \
|
||
|
type_t GetUpbValue<type_t>(upb_value val) { \
|
||
|
return upb_value_get##name(val); \
|
||
|
}
|
||
|
|
||
|
FUNCS(int32, int32_t, UPB_CTYPE_INT32)
|
||
|
FUNCS(int64, int64_t, UPB_CTYPE_INT64)
|
||
|
FUNCS(uint32, uint32_t, UPB_CTYPE_UINT32)
|
||
|
FUNCS(uint64, uint64_t, UPB_CTYPE_UINT64)
|
||
|
FUNCS(bool, bool, UPB_CTYPE_BOOL)
|
||
|
FUNCS(cstr, char*, UPB_CTYPE_CSTR)
|
||
|
FUNCS(ptr, void*, UPB_CTYPE_PTR)
|
||
|
FUNCS(constptr, const void*, UPB_CTYPE_CONSTPTR)
|
||
9 years ago
|
|
||
|
#undef FUNCS
|
||
|
|
||
|
class IntTable {
|
||
|
public:
|
||
4 years ago
|
IntTable() { upb_inttable_init(&table_, arena_.ptr()); }
|
||
9 years ago
|
|
||
|
size_t count() { return upb_inttable_count(&table_); }
|
||
|
|
||
|
bool Insert(uintptr_t key, upb_value val) {
|
||
4 years ago
|
return upb_inttable_insert(&table_, key, val, arena_.ptr());
|
||
9 years ago
|
}
|
||
|
|
||
|
bool Replace(uintptr_t key, upb_value val) {
|
||
|
return upb_inttable_replace(&table_, key, val);
|
||
|
}
|
||
|
|
||
|
std::pair<bool, upb_value> Remove(uintptr_t key) {
|
||
|
std::pair<bool, upb_value> ret;
|
||
|
ret.first = upb_inttable_remove(&table_, key, &ret.second);
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
std::pair<bool, upb_value> Lookup(uintptr_t key) const {
|
||
|
std::pair<bool, upb_value> ret;
|
||
|
ret.first = upb_inttable_lookup(&table_, key, &ret.second);
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
std::pair<bool, upb_value> Lookup32(uint32_t key) const {
|
||
|
std::pair<bool, upb_value> ret;
|
||
4 years ago
|
ret.first = upb_inttable_lookup(&table_, key, &ret.second);
|
||
9 years ago
|
return ret;
|
||
|
}
|
||
|
|
||
4 years ago
|
void Compact() { upb_inttable_compact(&table_, arena_.ptr()); }
|
||
9 years ago
|
|
||
|
class iterator : public std::iterator<std::forward_iterator_tag,
|
||
|
std::pair<uintptr_t, upb_value> > {
|
||
|
public:
|
||
|
explicit iterator(IntTable* table) {
|
||
|
upb_inttable_begin(&iter_, &table->table_);
|
||
|
}
|
||
|
|
||
|
static iterator end(IntTable* table) {
|
||
|
iterator iter(table);
|
||
|
upb_inttable_iter_setdone(&iter.iter_);
|
||
|
return iter;
|
||
|
}
|
||
|
|
||
3 years ago
|
void operator++() { return upb_inttable_next(&iter_); }
|
||
9 years ago
|
|
||
|
std::pair<uintptr_t, upb_value> operator*() const {
|
||
|
std::pair<uintptr_t, upb_value> ret;
|
||
|
ret.first = upb_inttable_iter_key(&iter_);
|
||
|
ret.second = upb_inttable_iter_value(&iter_);
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
bool operator==(const iterator& other) const {
|
||
|
return upb_inttable_iter_isequal(&iter_, &other.iter_);
|
||
|
}
|
||
|
|
||
3 years ago
|
bool operator!=(const iterator& other) const { return !(*this == other); }
|
||
9 years ago
|
|
||
|
private:
|
||
|
upb_inttable_iter iter_;
|
||
|
};
|
||
|
|
||
4 years ago
|
upb::Arena arena_;
|
||
9 years ago
|
upb_inttable table_;
|
||
|
};
|
||
|
|
||
|
class StrTable {
|
||
|
public:
|
||
4 years ago
|
StrTable() { upb_strtable_init(&table_, 4, arena_.ptr()); }
|
||
9 years ago
|
|
||
|
size_t count() { return upb_strtable_count(&table_); }
|
||
|
|
||
|
bool Insert(const std::string& key, upb_value val) {
|
||
4 years ago
|
return upb_strtable_insert(&table_, key.c_str(), key.size(), val,
|
||
|
arena_.ptr());
|
||
9 years ago
|
}
|
||
|
|
||
|
std::pair<bool, upb_value> Remove(const std::string& key) {
|
||
|
std::pair<bool, upb_value> ret;
|
||
|
ret.first =
|
||
3 years ago
|
upb_strtable_remove2(&table_, key.c_str(), key.size(), &ret.second);
|
||
9 years ago
|
return ret;
|
||
|
}
|
||
|
|
||
|
std::pair<bool, upb_value> Lookup(const std::string& key) const {
|
||
|
std::pair<bool, upb_value> ret;
|
||
|
ret.first =
|
||
|
upb_strtable_lookup2(&table_, key.c_str(), key.size(), &ret.second);
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
void Resize(size_t size_lg2) {
|
||
4 years ago
|
upb_strtable_resize(&table_, size_lg2, arena_.ptr());
|
||
9 years ago
|
}
|
||
|
|
||
|
class iterator : public std::iterator<std::forward_iterator_tag,
|
||
|
std::pair<std::string, upb_value> > {
|
||
|
public:
|
||
|
explicit iterator(StrTable* table) {
|
||
|
upb_strtable_begin(&iter_, &table->table_);
|
||
|
}
|
||
|
|
||
|
static iterator end(StrTable* table) {
|
||
|
iterator iter(table);
|
||
|
upb_strtable_iter_setdone(&iter.iter_);
|
||
|
return iter;
|
||
|
}
|
||
|
|
||
3 years ago
|
void operator++() { return upb_strtable_next(&iter_); }
|
||
9 years ago
|
|
||
|
std::pair<std::string, upb_value> operator*() const {
|
||
|
std::pair<std::string, upb_value> ret;
|
||
3 years ago
|
upb_StringView view = upb_strtable_iter_key(&iter_);
|
||
5 years ago
|
ret.first.assign(view.data, view.size);
|
||
9 years ago
|
ret.second = upb_strtable_iter_value(&iter_);
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
bool operator==(const iterator& other) const {
|
||
|
return upb_strtable_iter_isequal(&iter_, &other.iter_);
|
||
|
}
|
||
|
|
||
3 years ago
|
bool operator!=(const iterator& other) const { return !(*this == other); }
|
||
9 years ago
|
|
||
|
private:
|
||
|
upb_strtable_iter iter_;
|
||
|
};
|
||
|
|
||
4 years ago
|
upb::Arena arena_;
|
||
9 years ago
|
upb_strtable table_;
|
||
|
};
|
||
|
|
||
3 years ago
|
template <class T>
|
||
|
class TypedStrTable {
|
||
9 years ago
|
public:
|
||
|
size_t count() { return table_.count(); }
|
||
|
|
||
3 years ago
|
bool Insert(const std::string& key, T val) {
|
||
9 years ago
|
return table_.Insert(key, MakeUpbValue<T>(val));
|
||
|
}
|
||
|
|
||
|
std::pair<bool, T> Remove(const std::string& key) {
|
||
|
std::pair<bool, upb_value> found = table_.Remove(key);
|
||
|
std::pair<bool, T> ret;
|
||
|
ret.first = found.first;
|
||
|
if (ret.first) {
|
||
|
ret.second = GetUpbValue<T>(found.second);
|
||
|
}
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
std::pair<bool, T> Lookup(const std::string& key) const {
|
||
|
std::pair<bool, upb_value> found = table_.Lookup(key);
|
||
|
std::pair<bool, T> ret;
|
||
|
ret.first = found.first;
|
||
|
if (ret.first) {
|
||
|
ret.second = GetUpbValue<T>(found.second);
|
||
|
}
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
3 years ago
|
void Resize(size_t size_lg2) { table_.Resize(size_lg2); }
|
||
9 years ago
|
|
||
3 years ago
|
class iterator : public std::iterator<std::forward_iterator_tag,
|
||
|
std::pair<std::string, T> > {
|
||
9 years ago
|
public:
|
||
|
explicit iterator(TypedStrTable* table) : iter_(&table->table_) {}
|
||
|
static iterator end(TypedStrTable* table) {
|
||
|
iterator iter(table);
|
||
|
iter.iter_ = StrTable::iterator::end(&table->table_);
|
||
|
return iter;
|
||
|
}
|
||
|
|
||
|
void operator++() { ++iter_; }
|
||
|
|
||
|
std::pair<std::string, T> operator*() const {
|
||
|
std::pair<std::string, upb_value> val = *iter_;
|
||
|
std::pair<std::string, T> ret;
|
||
|
ret.first = val.first;
|
||
|
ret.second = GetUpbValue<T>(val.second);
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
bool operator==(const iterator& other) const {
|
||
|
return iter_ == other.iter_;
|
||
|
}
|
||
|
|
||
|
bool operator!=(const iterator& other) const {
|
||
|
return iter_ != other.iter_;
|
||
|
}
|
||
|
|
||
|
private:
|
||
|
StrTable::iterator iter_;
|
||
|
};
|
||
|
|
||
|
iterator begin() { return iterator(this); }
|
||
|
iterator end() { return iterator::end(this); }
|
||
|
|
||
|
StrTable table_;
|
||
|
};
|
||
|
|
||
3 years ago
|
template <class T>
|
||
|
class TypedIntTable {
|
||
9 years ago
|
public:
|
||
|
size_t count() { return table_.count(); }
|
||
|
|
||
|
bool Insert(uintptr_t key, T val) {
|
||
|
return table_.Insert(key, MakeUpbValue<T>(val));
|
||
|
}
|
||
|
|
||
|
bool Replace(uintptr_t key, T val) {
|
||
|
return table_.Replace(key, MakeUpbValue<T>(val));
|
||
|
}
|
||
|
|
||
|
std::pair<bool, T> Remove(uintptr_t key) {
|
||
|
std::pair<bool, upb_value> found = table_.Remove(key);
|
||
|
std::pair<bool, T> ret;
|
||
|
ret.first = found.first;
|
||
|
if (ret.first) {
|
||
|
ret.second = GetUpbValue<T>(found.second);
|
||
|
}
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
std::pair<bool, T> Lookup(uintptr_t key) const {
|
||
|
std::pair<bool, upb_value> found = table_.Lookup(key);
|
||
|
std::pair<bool, T> ret;
|
||
|
ret.first = found.first;
|
||
|
if (ret.first) {
|
||
|
ret.second = GetUpbValue<T>(found.second);
|
||
|
}
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
void Compact() { table_.Compact(); }
|
||
|
|
||
3 years ago
|
class iterator : public std::iterator<std::forward_iterator_tag,
|
||
|
std::pair<uintptr_t, T> > {
|
||
9 years ago
|
public:
|
||
|
explicit iterator(TypedIntTable* table) : iter_(&table->table_) {}
|
||
|
static iterator end(TypedIntTable* table) {
|
||
|
return IntTable::iterator::end(&table->table_);
|
||
|
}
|
||
|
|
||
|
void operator++() { ++iter_; }
|
||
|
|
||
|
std::pair<uintptr_t, T> operator*() const {
|
||
|
std::pair<uintptr_t, upb_value> val = *iter_;
|
||
|
std::pair<uintptr_t, T> ret;
|
||
|
ret.first = val.first;
|
||
|
ret.second = GetUpbValue<T>(val.second);
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
bool operator==(const iterator& other) const {
|
||
|
return iter_ == other.iter_;
|
||
|
}
|
||
|
|
||
|
bool operator!=(const iterator& other) const {
|
||
|
return iter_ != other.iter_;
|
||
|
}
|
||
|
|
||
|
private:
|
||
|
IntTable::iterator iter_;
|
||
|
};
|
||
|
|
||
|
iterator begin() { return iterator(this); }
|
||
|
iterator end() { return iterator::end(this); }
|
||
|
|
||
|
IntTable table_;
|
||
|
};
|
||
|
|
||
3 years ago
|
} // namespace upb
|
||
9 years ago
|
|
||
14 years ago
|
#define CPU_TIME_PER_TEST 0.5
|
||
15 years ago
|
|
||
16 years ago
|
using std::vector;
|
||
|
|
||
13 years ago
|
double get_usertime() {
|
||
16 years ago
|
struct rusage usage;
|
||
|
getrusage(RUSAGE_SELF, &usage);
|
||
3 years ago
|
return usage.ru_utime.tv_sec + (usage.ru_utime.tv_usec / 1000000.0);
|
||
16 years ago
|
}
|
||
|
|
||
3 years ago
|
TEST(Table, StringTable) {
|
||
|
vector<std::string> keys;
|
||
|
keys.push_back("google.protobuf.FileDescriptorSet");
|
||
|
keys.push_back("google.protobuf.FileDescriptorProto");
|
||
|
keys.push_back("google.protobuf.DescriptorProto");
|
||
|
keys.push_back("google.protobuf.DescriptorProto.ExtensionRange");
|
||
|
keys.push_back("google.protobuf.FieldDescriptorProto");
|
||
|
keys.push_back("google.protobuf.EnumDescriptorProto");
|
||
|
keys.push_back("google.protobuf.EnumValueDescriptorProto");
|
||
|
keys.push_back("google.protobuf.ServiceDescriptorProto");
|
||
|
keys.push_back("google.protobuf.MethodDescriptorProto");
|
||
|
keys.push_back("google.protobuf.FileOptions");
|
||
|
keys.push_back("google.protobuf.MessageOptions");
|
||
|
keys.push_back("google.protobuf.FieldOptions");
|
||
|
keys.push_back("google.protobuf.EnumOptions");
|
||
|
keys.push_back("google.protobuf.EnumValueOptions");
|
||
|
keys.push_back("google.protobuf.ServiceOptions");
|
||
|
keys.push_back("google.protobuf.MethodOptions");
|
||
|
keys.push_back("google.protobuf.UninterpretedOption");
|
||
|
keys.push_back("google.protobuf.UninterpretedOption.NamePart");
|
||
|
|
||
16 years ago
|
/* Initialize structures. */
|
||
13 years ago
|
std::map<std::string, int32_t> m;
|
||
9 years ago
|
typedef upb::TypedStrTable<int32_t> Table;
|
||
|
Table table;
|
||
13 years ago
|
std::set<std::string> all;
|
||
3 years ago
|
for (const auto& key : keys) {
|
||
16 years ago
|
all.insert(key);
|
||
9 years ago
|
table.Insert(key, key[0]);
|
||
16 years ago
|
m[key] = key[0];
|
||
|
}
|
||
|
|
||
|
/* Test correctness. */
|
||
3 years ago
|
for (const auto& key : keys) {
|
||
9 years ago
|
std::pair<bool, int32_t> found = table.Lookup(key);
|
||
3 years ago
|
if (m.find(key) != m.end()) { /* Assume map implementation is correct. */
|
||
3 years ago
|
EXPECT_TRUE(found.first);
|
||
|
EXPECT_EQ(found.second, key[0]);
|
||
|
EXPECT_EQ(m[key], key[0]);
|
||
16 years ago
|
} else {
|
||
3 years ago
|
EXPECT_FALSE(found.first);
|
||
16 years ago
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
for (Table::iterator it = table.begin(); it != table.end(); ++it) {
|
||
|
std::set<std::string>::iterator i = all.find((*it).first);
|
||
3 years ago
|
EXPECT_NE(i, all.end());
|
||
16 years ago
|
all.erase(i);
|
||
|
}
|
||
3 years ago
|
EXPECT_TRUE(all.empty());
|
||
16 years ago
|
|
||
11 years ago
|
// Test iteration with resizes.
|
||
|
|
||
|
for (int i = 0; i < 10; i++) {
|
||
9 years ago
|
for (Table::iterator it = table.begin(); it != table.end(); ++it) {
|
||
11 years ago
|
// Even if we invalidate the iterator it should only return real elements.
|
||
3 years ago
|
EXPECT_EQ((*it).second, m[(*it).first]);
|
||
11 years ago
|
|
||
|
// Force a resize even though the size isn't changing.
|
||
|
// Also forces the table size to grow so some new buckets end up empty.
|
||
9 years ago
|
int new_lg2 = table.table_.table_.t.size_lg2 + 1;
|
||
11 years ago
|
// Don't use more than 64k tables, to avoid exhausting memory.
|
||
|
new_lg2 = UPB_MIN(new_lg2, 16);
|
||
9 years ago
|
table.Resize(new_lg2);
|
||
11 years ago
|
}
|
||
|
}
|
||
16 years ago
|
}
|
||
16 years ago
|
|
||
3 years ago
|
class IntTableTest : public testing::TestWithParam<int> {
|
||
|
void SetUp() override {
|
||
|
if (GetParam() > 0) {
|
||
|
for (int i = 0; i < GetParam(); i++) {
|
||
|
keys_.push_back(i + 1);
|
||
|
}
|
||
|
} else {
|
||
|
for (int32_t i = 0; i < 64; i++) {
|
||
|
if (i < 32)
|
||
|
keys_.push_back(i + 1);
|
||
|
else
|
||
|
keys_.push_back(10101 + i);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected:
|
||
|
std::vector<int32_t> keys_;
|
||
|
};
|
||
|
|
||
|
TEST_P(IntTableTest, TestIntTable) {
|
||
16 years ago
|
/* Initialize structures. */
|
||
9 years ago
|
typedef upb::TypedIntTable<uint32_t> Table;
|
||
|
Table table;
|
||
16 years ago
|
uint32_t largest_key = 0;
|
||
|
std::map<uint32_t, uint32_t> m;
|
||
6 years ago
|
std::unordered_map<uint32_t, uint32_t> hm;
|
||
3 years ago
|
for (const auto& key : keys_) {
|
||
16 years ago
|
largest_key = UPB_MAX((int32_t)largest_key, key);
|
||
9 years ago
|
table.Insert(key, key * 2);
|
||
3 years ago
|
m[key] = key * 2;
|
||
|
hm[key] = key * 2;
|
||
16 years ago
|
}
|
||
|
|
||
|
/* Test correctness. */
|
||
3 years ago
|
for (uint32_t i = 0; i <= largest_key; i++) {
|
||
9 years ago
|
std::pair<bool, uint32_t> found = table.Lookup(i);
|
||
3 years ago
|
if (m.find(i) != m.end()) { /* Assume map implementation is correct. */
|
||
3 years ago
|
EXPECT_TRUE(found.first);
|
||
|
EXPECT_EQ(found.second, i * 2);
|
||
|
EXPECT_EQ(m[i], i * 2);
|
||
|
EXPECT_EQ(hm[i], i * 2);
|
||
13 years ago
|
} else {
|
||
3 years ago
|
EXPECT_FALSE(found.first);
|
||
13 years ago
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
for (size_t i = 0; i < keys_.size(); i += 2) {
|
||
|
std::pair<bool, uint32_t> found = table.Remove(keys_[i]);
|
||
|
EXPECT_EQ(found.first, m.erase(keys_[i]) == 1);
|
||
3 years ago
|
if (found.first) {
|
||
|
EXPECT_EQ(found.second, (uint32_t)keys_[i] * 2);
|
||
|
}
|
||
3 years ago
|
hm.erase(keys_[i]);
|
||
|
m.erase(keys_[i]);
|
||
13 years ago
|
}
|
||
|
|
||
3 years ago
|
EXPECT_EQ(table.count(), hm.size());
|
||
13 years ago
|
|
||
|
/* Test correctness. */
|
||
3 years ago
|
for (uint32_t i = 0; i <= largest_key; i++) {
|
||
9 years ago
|
std::pair<bool, uint32_t> found = table.Lookup(i);
|
||
3 years ago
|
if (m.find(i) != m.end()) { /* Assume map implementation is correct. */
|
||
3 years ago
|
EXPECT_TRUE(found.first);
|
||
|
EXPECT_EQ(found.second, i * 2);
|
||
|
EXPECT_EQ(m[i], i * 2);
|
||
|
EXPECT_EQ(hm[i], i * 2);
|
||
14 years ago
|
} else {
|
||
3 years ago
|
EXPECT_FALSE(found.first);
|
||
14 years ago
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
// Test replace.
|
||
3 years ago
|
for (uint32_t i = 0; i <= largest_key; i++) {
|
||
|
bool replaced = table.Replace(i, i * 3);
|
||
|
if (m.find(i) != m.end()) { /* Assume map implementation is correct. */
|
||
3 years ago
|
EXPECT_TRUE(replaced);
|
||
11 years ago
|
m[i] = i * 3;
|
||
|
hm[i] = i * 3;
|
||
|
} else {
|
||
3 years ago
|
EXPECT_FALSE(replaced);
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
// Compact and test correctness again.
|
||
9 years ago
|
table.Compact();
|
||
3 years ago
|
for (uint32_t i = 0; i <= largest_key; i++) {
|
||
9 years ago
|
std::pair<bool, uint32_t> found = table.Lookup(i);
|
||
3 years ago
|
if (m.find(i) != m.end()) { /* Assume map implementation is correct. */
|
||
3 years ago
|
EXPECT_TRUE(found.first);
|
||
|
EXPECT_EQ(found.second, i * 3);
|
||
|
EXPECT_EQ(m[i], i * 3);
|
||
|
EXPECT_EQ(hm[i], i * 3);
|
||
16 years ago
|
} else {
|
||
3 years ago
|
EXPECT_FALSE(found.first);
|
||
16 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
INSTANTIATE_TEST_SUITE_P(IntTableParams, IntTableTest,
|
||
|
testing::Values(8, 64, 512, -32));
|
||
|
|
||
9 years ago
|
/*
|
||
|
* This test can't pass right now because the table can't store a value of
|
||
|
* (uint64_t)-1.
|
||
|
*/
|
||
3 years ago
|
TEST(Table, MaxValue) {
|
||
3 years ago
|
/*
|
||
|
typedef upb::TypedIntTable<uint64_t> Table;
|
||
|
Table table;
|
||
|
uintptr_t uint64_max = (uint64_t)-1;
|
||
|
table.Insert(1, uint64_max);
|
||
|
std::pair<bool, uint64_t> found = table.Lookup(1);
|
||
|
ASSERT(found.first);
|
||
|
ASSERT(found.second == uint64_max);
|
||
|
*/
|
||
9 years ago
|
}
|
||
|
|
||
3 years ago
|
TEST(Table, Delete) {
|
||
4 years ago
|
upb::Arena arena;
|
||
11 years ago
|
upb_inttable t;
|
||
4 years ago
|
upb_inttable_init(&t, arena.ptr());
|
||
|
upb_inttable_insert(&t, 0, upb_value_bool(true), arena.ptr());
|
||
|
upb_inttable_insert(&t, 2, upb_value_bool(true), arena.ptr());
|
||
|
upb_inttable_insert(&t, 4, upb_value_bool(true), arena.ptr());
|
||
|
upb_inttable_compact(&t, arena.ptr());
|
||
11 years ago
|
upb_inttable_remove(&t, 0, NULL);
|
||
|
upb_inttable_remove(&t, 2, NULL);
|
||
|
upb_inttable_remove(&t, 4, NULL);
|
||
|
|
||
|
upb_inttable_iter iter;
|
||
|
for (upb_inttable_begin(&iter, &t); !upb_inttable_done(&iter);
|
||
|
upb_inttable_next(&iter)) {
|
||
3 years ago
|
ASSERT_TRUE(false);
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
TEST(Table, Init) {
|
||
4 years ago
|
for (int i = 0; i < 2048; i++) {
|
||
|
/* Tests that the size calculations in init() (lg2 size for target load)
|
||
|
* work for all expected sizes. */
|
||
4 years ago
|
upb::Arena arena;
|
||
4 years ago
|
upb_strtable t;
|
||
4 years ago
|
upb_strtable_init(&t, i, arena.ptr());
|
||
4 years ago
|
}
|
||
|
}
|