Conversation
|
|
||
| TEST(ResourceTest, Type) { | ||
| std::map<std::string, std::string> m; | ||
| google::MonitoredResource mr("type", m); |
There was a problem hiding this comment.
[Optional] Let's go with something other than "type", to make sure the field name does not accidentally leak into the value. I would propose something like "some_resource".
There was a problem hiding this comment.
Good idea, changed.
| } | ||
|
|
||
| TEST(ResourceTest, Labels) { | ||
| std::map<std::string, std::string> m; |
There was a problem hiding this comment.
std::map<std::string, std::string> m{{"foo", "bar"}, {"bar", "baz"}};
google::MonitoredResource mr("", m);or even
google::MonitoredResource mr("", {{"foo", "bar"}, {"bar", "baz"}});There was a problem hiding this comment.
Great, changed everywhere.
| TEST(ResourceTest, BasicEquality) { | ||
| std::map<std::string, std::string> m1; | ||
| std::map<std::string, std::string> m2; | ||
| google::MonitoredResource mr1("", m1); |
There was a problem hiding this comment.
google::MonitoredResource mr1("", {});
google::MonitoredResource mr2("", {});There was a problem hiding this comment.
Changed everywhere.
| google::MonitoredResource mr1("", m1); | ||
| google::MonitoredResource mr2("", m2); | ||
|
|
||
| EXPECT_TRUE(mr1 == mr2); |
| google::MonitoredResource mr1("2", m1); | ||
| google::MonitoredResource mr2("1", m2); | ||
|
|
||
| EXPECT_TRUE(mr1 < mr2); |
| google::MonitoredResource mr1("", m1); | ||
| google::MonitoredResource mr2("", m2); | ||
|
|
||
| EXPECT_FALSE(mr1 == mr2); |
There was a problem hiding this comment.
Changed. I had to add the != operator to resource, but luckily the NE test will cover that as well.
7a96877 to
eeeb6e6
Compare
|
|
||
| TEST(ResourceTest, Type) { | ||
| std::map<std::string, std::string> m; | ||
| google::MonitoredResource mr("some_resource", m); |
There was a problem hiding this comment.
google::MonitoredResource mr("some_resource", {}).
| google::MonitoredResource mr1("2", {}); | ||
| google::MonitoredResource mr2("1", {}); | ||
|
|
||
| EXPECT_LT(mr1, mr2); |
There was a problem hiding this comment.
This one confuses me, mr1's key is 2, how is 2 less than 1?
There was a problem hiding this comment.
Simple -- it's a bug; the implementation is inverted. :-)
Yay for unit tests?
There was a problem hiding this comment.
Not a bug: "2" < "1" and "b" < "a". This is confusing, so I added simple examples above the expects to make it more clear.
There was a problem hiding this comment.
Let's be careful here. The specific implementation of operator< for resources does have a bug, because x < y becomes x.operator<(y), which, in the current implementation would evaluate y.type_ < x.type_ || (y.type_ == x.type_ && y.labels_ < x.labels_), which is clearly wrong.
The test @ACEmilG ran to get "2" < "1" is flawed. If you compare std::strings, it would behave as expected: std::string("1") < std::string("2"). When you compare char* values, you're comparing pointers, so the order is essentially random, so that doesn't count.
There was a problem hiding this comment.
I see, thanks for clarification!
There was a problem hiding this comment.
Sure. It's funny that ultimately, the behavior of the operator didn't matter as long as it's stable, because all it was needed for was using MonitoredResource as a key in std::map. But it's good to get it right.
| google::MonitoredResource mr2("", {{"a", "a"}}); | ||
|
|
||
| EXPECT_NE(mr1, mr2); | ||
| EXPECT_LT(mr1, mr2); |
| google::MonitoredResource mr1("2", {}); | ||
| google::MonitoredResource mr2("1", {}); | ||
|
|
||
| EXPECT_LT(mr1, mr2); |
There was a problem hiding this comment.
Let's be careful here. The specific implementation of operator< for resources does have a bug, because x < y becomes x.operator<(y), which, in the current implementation would evaluate y.type_ < x.type_ || (y.type_ == x.type_ && y.labels_ < x.labels_), which is clearly wrong.
The test @ACEmilG ran to get "2" < "1" is flawed. If you compare std::strings, it would behave as expected: std::string("1") < std::string("2"). When you compare char* values, you're comparing pointers, so the order is essentially random, so that doesn't count.
| google::MonitoredResource mr1("2", {}); | ||
| google::MonitoredResource mr2("1", {}); | ||
|
|
||
| EXPECT_LT("2", "1"); |
There was a problem hiding this comment.
Leftover debug code?
There was a problem hiding this comment.
Removed. It was as an example (for my mistaken understanding of the comparison)
| google::MonitoredResource mr1("", {{"b", "b"}}); | ||
| google::MonitoredResource mr2("", {{"a", "a"}}); | ||
|
|
||
| EXPECT_LT("b", "a"); |
There was a problem hiding this comment.
Leftover debug code?
igorpeshansky
left a comment
There was a problem hiding this comment.
LGTM ![]()
Don't forget to "squash and merge".
| google::MonitoredResource mr1("2", {}); | ||
| google::MonitoredResource mr2("1", {}); | ||
|
|
||
| EXPECT_LT(mr1, mr2); |
There was a problem hiding this comment.
Sure. It's funny that ultimately, the behavior of the operator didn't matter as long as it's stable, because all it was needed for was using MonitoredResource as a key in std::map. But it's good to get it right.
No description provided.