Skip to content

Commit

Permalink
Use a human-readable string format for TypeInfo in inference_table_…
Browse files Browse the repository at this point in the history
…test.

The problem with the proto format is it's overly verbose for this purpose, and yet doesn't contain enough info to easily tell which node one is reading about. That is why type_info_to_proto_test itself does not even assert against the proto.

PiperOrigin-RevId: 698450507
  • Loading branch information
richmckeever authored and copybara-github committed Nov 20, 2024
1 parent 7dad99e commit fb74958
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 68 deletions.
4 changes: 2 additions & 2 deletions xls/dslx/type_system_v2/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,17 @@ cc_test(
deps = [
":inference_table",
":inference_table_to_type_info",
"//xls/common:proto_test_utils",
"//xls/common:xls_gunit_main",
"//xls/common/status:matchers",
"//xls/common/status:status_macros",
"//xls/dslx/frontend:ast",
"//xls/dslx/frontend:module",
"//xls/dslx/frontend:pos",
"//xls/dslx/type_system:type_info",
"//xls/dslx/type_system:type_info_to_proto",
"@com_google_absl//absl/algorithm:container",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_googletest//:gtest",
],
)
103 changes: 37 additions & 66 deletions xls/dslx/type_system_v2/inference_table_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,29 @@

#include <memory>
#include <optional>
#include <string>
#include <vector>

#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/algorithm/container.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "xls/common/proto_test_utils.h"
#include "absl/strings/str_join.h"
#include "absl/strings/substitute.h"
#include "xls/common/status/matchers.h"
#include "xls/common/status/status_macros.h"
#include "xls/dslx/frontend/ast.h"
#include "xls/dslx/frontend/module.h"
#include "xls/dslx/frontend/pos.h"
#include "xls/dslx/type_system/type_info.h"
#include "xls/dslx/type_system/type_info_to_proto.h"
#include "xls/dslx/type_system_v2/inference_table_to_type_info.h"

namespace xls::dslx {
namespace {

using ::absl_testing::StatusIs;
using ::testing::ContainsRegex;
using ::xls::proto_testing::EqualsProto;

class InferenceTableTest : public ::testing::Test {
public:
Expand All @@ -46,11 +48,28 @@ class InferenceTableTest : public ::testing::Test {
table_ = InferenceTable::Create(*module_, file_table_);
}

absl::StatusOr<TypeInfoProto> ConvertTableToProto() {
absl::StatusOr<std::string> TypeInfoToString(const TypeInfo& ti) {
if (ti.dict().empty()) {
return "";
}
std::vector<std::string> strings;
for (const auto& [node, type] : ti.dict()) {
Span span = node->GetSpan().has_value() ? *node->GetSpan() : Span::Fake();
strings.push_back(absl::Substitute("span: $0, node: `$1`, type: $2",
span.ToString(file_table_),
node->ToString(), type->ToString()));
}
absl::c_sort(strings);
return strings.size() == 1
? strings[0]
: absl::Substitute("\n$0\n", absl::StrJoin(strings, "\n"));
}

absl::StatusOr<std::string> ConvertTableToTypeInfoString() {
XLS_ASSIGN_OR_RETURN(
TypeInfo * ti, InferenceTableToTypeInfo(*table_, *module_,
type_info_owner_, file_table_));
return TypeInfoToProto(*ti);
return TypeInfoToString(*ti);
}

FileTable file_table_;
Expand All @@ -60,8 +79,9 @@ class InferenceTableTest : public ::testing::Test {
};

TEST_F(InferenceTableTest, TypeInfoForEmptyTable) {
XLS_ASSERT_OK_AND_ASSIGN(TypeInfoProto proto, ConvertTableToProto());
EXPECT_THAT(proto, EqualsProto(""));
XLS_ASSERT_OK_AND_ASSIGN(std::string type_info_string,
ConvertTableToTypeInfoString());
EXPECT_EQ(type_info_string, "");
}

TEST_F(InferenceTableTest, TypeInfoForOneSimpleAnnotation) {
Expand All @@ -71,27 +91,10 @@ TEST_F(InferenceTableTest, TypeInfoForOneSimpleAnnotation) {
Span::Fake(), BuiltinType::kU32,
module_->GetOrCreateBuiltinNameDef("u32"));
XLS_EXPECT_OK(table_->SetTypeAnnotation(x, annotation));
XLS_ASSERT_OK_AND_ASSIGN(TypeInfoProto proto, ConvertTableToProto());
EXPECT_THAT(
proto, EqualsProto(R"pb(
nodes {
kind: AST_NODE_KIND_NAME_DEF
span {
start { filename: "<no-file>" lineno: 0 colno: 0 }
limit { filename: "<no-file>" lineno: 0 colno: 0 }
}
type {
bits_type {
is_signed: false
dim {
interp_value {
bits { is_signed: false bit_count: 32 data: "\000\000\000 " }
}
}
}
}
}
)pb"));
XLS_ASSERT_OK_AND_ASSIGN(std::string type_info_string,
ConvertTableToTypeInfoString());
EXPECT_EQ(type_info_string,
"span: <no-file>:1:1-1:1, node: `x`, type: uN[32]");
}

TEST_F(InferenceTableTest, SetTypeVariableToNonInferenceVariable) {
Expand Down Expand Up @@ -169,44 +172,12 @@ TEST_F(InferenceTableTest, SignednessAgreement) {
XLS_EXPECT_OK(table_->SetTypeVariable(y_ref, t0));
XLS_EXPECT_OK(table_->SetTypeAnnotation(x_ref, u32_annotation));
XLS_EXPECT_OK(table_->SetTypeAnnotation(y_ref, u32_annotation));
XLS_ASSERT_OK_AND_ASSIGN(TypeInfoProto proto, ConvertTableToProto());
EXPECT_THAT(
proto, EqualsProto(R"pb(
nodes {
kind: AST_NODE_KIND_NAME_REF
span {
start { filename: "<no-file>" lineno: 0 colno: 0 }
limit { filename: "<no-file>" lineno: 0 colno: 0 }
}
type {
bits_type {
is_signed: false
dim {
interp_value {
bits { is_signed: false bit_count: 32 data: "\000\000\000 " }
}
}
}
}
}
nodes {
kind: AST_NODE_KIND_NAME_REF
span {
start { filename: "<no-file>" lineno: 0 colno: 0 }
limit { filename: "<no-file>" lineno: 0 colno: 0 }
}
type {
bits_type {
is_signed: false
dim {
interp_value {
bits { is_signed: false bit_count: 32 data: "\000\000\000 " }
}
}
}
}
}
)pb"));
XLS_ASSERT_OK_AND_ASSIGN(std::string type_info_string,
ConvertTableToTypeInfoString());
EXPECT_EQ(type_info_string, R"(
span: <no-file>:1:1-1:1, node: `x`, type: uN[32]
span: <no-file>:1:1-1:1, node: `y`, type: uN[32]
)");
}

} // namespace
Expand Down

0 comments on commit fb74958

Please sign in to comment.