Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions xls/ir/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -1271,7 +1271,11 @@ cc_test(
":bits",
":value",
":value_test_util",
"//xls/common:proto_test_utils",
"//xls/common:xls_gunit_main",
"//xls/common/file:filesystem",
"//xls/common/fuzzing:fuzztest",
"@abseil-cpp//absl/log:check",
"@googletest//:gtest",
],
)
Expand Down Expand Up @@ -1354,9 +1358,12 @@ cc_library(
":type_manager",
":value",
":value_flattening",
":xls_ir_interface_cc_proto",
":xls_type_cc_proto",
"//xls/common/file:filesystem",
"//xls/common/fuzzing:fuzztest",
"@abseil-cpp//absl/log:check",
"@abseil-cpp//absl/types:span",
"@googletest//:gtest",
],
)
Expand Down
25 changes: 25 additions & 0 deletions xls/ir/value_test_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,21 @@
#include "xls/ir/value_test_util.h"

#include <cstdint>
#include <utility>
#include <vector>

#include "gtest/gtest.h"
#include "xls/common/fuzzing/fuzztest.h"
#include "absl/log/check.h"
#include "xls/common/file/filesystem.h"
#include "xls/ir/bits.h"
#include "xls/ir/bits_test_utils.h"
#include "xls/ir/fuzz_type_domain.h"
#include "xls/ir/type.h"
#include "xls/ir/type_manager.h"
#include "xls/ir/value.h"
#include "xls/ir/value_flattening.h"
#include "xls/ir/xls_ir_interface.pb.h"
#include "xls/ir/xls_type.pb.h"

namespace xls {
Expand Down Expand Up @@ -75,4 +79,25 @@ fuzztest::Domain<Value> ArbitraryValue(TypeProto type) {
return ArbitraryValue(fuzztest::Just(type));
}

// Parses the binary-format serialized proto of type ElementOfProto and returns
// the proto.
ElementOfProto ParseElementOfProto(absl::Span<const uint8_t> bytes) {
ElementOfProto proto;
CHECK(proto.ParseFromArray(bytes.data(), bytes.size()));
return proto;
}

// Note to self: ElementOfProto is a 'using' alias defined in the header.
fuzztest::Domain<Value> ElementOfDomain(ElementOfProto proto) {
std::vector<Value> values;
values.reserve(proto.values_size());
for (const auto& value_proto : proto.values()) {
auto value_or = Value::FromProto(value_proto);
CHECK_OK(value_or.status()) << "Failed to parse Value from proto: "
<< value_proto.ShortDebugString();
values.push_back(std::move(value_or.value()));
}
return fuzztest::ElementOf(values);
}

} // namespace xls
11 changes: 11 additions & 0 deletions xls/ir/value_test_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@

#include "gtest/gtest.h"
#include "xls/common/fuzzing/fuzztest.h"
#include "absl/types/span.h"
#include "xls/ir/value.h"
#include "xls/ir/xls_ir_interface.pb.h"
#include "xls/ir/xls_type.pb.h"

namespace xls {

using ElementOfProto = PackageInterfaceProto::FuzzTestDomain::ElementOf;

// Returns an assertion result indicating whether the given two values were
// equal. If equal the return value is AssertionSuccess, otherwise
// AssertionFailure. For large Values (arrays, tuples, and very wide bit widths)
Expand All @@ -43,6 +47,13 @@ fuzztest::Domain<Value> ArbitraryValue(fuzztest::Domain<TypeProto> type);
// Create a domain for an arbitrary value which is of the given type.
fuzztest::Domain<Value> ArbitraryValue(TypeProto type);

// Parses the binary-format serialized proto of type ElementOfProto and returns
// the proto.
ElementOfProto ParseElementOfProto(absl::Span<const uint8_t> bytes);

// Create an element_of domain from a serialized ElementOf proto.
fuzztest::Domain<Value> ElementOfDomain(ElementOfProto proto);

} // namespace xls

#endif // XLS_IR_VALUE_TEST_UTIL_H_
26 changes: 26 additions & 0 deletions xls/ir/value_test_util_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@

#include "xls/ir/value_test_util.h"

#include <cstdint>

#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "xls/common/fuzzing/fuzztest.h"
#include "xls/common/proto_test_utils.h"
#include "xls/ir/bits.h"
#include "xls/ir/value.h"

Expand All @@ -28,5 +33,26 @@ TEST(ValueTestUtilTest, ValuesEqual) {
EXPECT_FALSE(ValuesEqual(Value(UBits(1, 1234)), Value(UBits(1, 10))));
}

ElementOfProto MakeTestProto() {
ElementOfProto proto;
*proto.add_values() = Value(UBits(1, 32)).AsProto().value();
*proto.add_values() = Value(UBits(2, 32)).AsProto().value();
return proto;
}

void ElementOfDomainTestHelper(const Value& value) {
EXPECT_TRUE(value == Value(UBits(1, 32)) || value == Value(UBits(2, 32)));
}
FUZZ_TEST(ValueTestUtilTest, ElementOfDomainTestHelper)
.WithDomains(ElementOfDomain(MakeTestProto()));

TEST(ValueTestUtilTest, ParseElementOfProtoBytes) {
ElementOfProto original = MakeTestProto();
std::string serialized = original.SerializeAsString();
std::vector<uint8_t> bytes(serialized.begin(), serialized.end());
ElementOfProto parsed = ParseElementOfProto(bytes);
EXPECT_THAT(parsed, proto_testing::EqualsProto(original));
}

} // namespace
} // namespace xls
3 changes: 2 additions & 1 deletion xls/jit/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ pytype_strict_library(
"//xls/ir:xls_ir_interface_py_pb2",
"//xls/ir:xls_type_py_pb2",
"@abseil-py//absl:app",
"@protobuf//:protobuf_python",
"@xls_pip_deps//jinja2",
],
)
Expand Down Expand Up @@ -413,8 +414,8 @@ pytype_strict_contrib_test(
"//xls/common:runfiles",
"//xls/ir:xls_ir_interface_py_pb2",
"//xls/ir:xls_type_py_pb2",
"@abseil-py//absl:app",
"@abseil-py//absl/testing:absltest",
"@protobuf//:protobuf_python",
"@xls_pip_deps//jinja2",
],
)
Expand Down
Loading
Loading