2021-10-30 04:25:12 +08:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
#include "Luau/Symbol.h"
|
|
|
|
#include "Luau/Ast.h"
|
|
|
|
|
|
|
|
#include "Fixture.h"
|
|
|
|
|
|
|
|
#include "doctest.h"
|
|
|
|
|
|
|
|
using namespace Luau;
|
|
|
|
|
2023-12-02 15:46:57 +08:00
|
|
|
LUAU_FASTFLAG(DebugLuauDeferredConstraintResolution)
|
|
|
|
|
2021-10-30 04:25:12 +08:00
|
|
|
TEST_SUITE_BEGIN("SymbolTests");
|
|
|
|
|
2022-10-22 01:54:01 +08:00
|
|
|
TEST_CASE("equality_and_hashing_of_globals")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
std::string s1 = "name";
|
|
|
|
std::string s2 = "name";
|
|
|
|
|
|
|
|
// These two names point to distinct memory areas.
|
|
|
|
AstName one{s1.data()};
|
|
|
|
AstName two{s2.data()};
|
|
|
|
|
|
|
|
Symbol n1{one};
|
|
|
|
Symbol n2{two};
|
|
|
|
|
|
|
|
CHECK(n1 == n1);
|
|
|
|
CHECK(n1 == n2);
|
|
|
|
CHECK(n2 == n2);
|
|
|
|
|
|
|
|
CHECK_EQ(std::hash<Symbol>()(one), std::hash<Symbol>()(one));
|
|
|
|
CHECK_EQ(std::hash<Symbol>()(one), std::hash<Symbol>()(two));
|
|
|
|
CHECK_EQ(std::hash<Symbol>()(two), std::hash<Symbol>()(two));
|
|
|
|
|
|
|
|
std::unordered_map<Symbol, int> theMap;
|
2022-01-07 07:26:14 +08:00
|
|
|
theMap[n1] = 5;
|
|
|
|
theMap[n2] = 1;
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
REQUIRE_EQ(1, theMap.size());
|
|
|
|
}
|
|
|
|
|
2022-10-22 01:54:01 +08:00
|
|
|
TEST_CASE("equality_and_hashing_of_locals")
|
2022-01-07 07:26:14 +08:00
|
|
|
{
|
|
|
|
std::string s1 = "name";
|
|
|
|
std::string s2 = "name";
|
|
|
|
|
|
|
|
// These two names point to distinct memory areas.
|
|
|
|
AstLocal one{AstName{s1.data()}, Location(), nullptr, 0, 0, nullptr};
|
|
|
|
AstLocal two{AstName{s2.data()}, Location(), &one, 0, 0, nullptr};
|
|
|
|
|
|
|
|
Symbol n1{&one};
|
|
|
|
Symbol n2{&two};
|
|
|
|
|
|
|
|
CHECK(n1 == n1);
|
|
|
|
CHECK(n1 != n2);
|
|
|
|
CHECK(n2 == n2);
|
|
|
|
|
|
|
|
CHECK_EQ(std::hash<Symbol>()(&one), std::hash<Symbol>()(&one));
|
|
|
|
CHECK_NE(std::hash<Symbol>()(&one), std::hash<Symbol>()(&two));
|
|
|
|
CHECK_EQ(std::hash<Symbol>()(&two), std::hash<Symbol>()(&two));
|
|
|
|
|
|
|
|
std::unordered_map<Symbol, int> theMap;
|
|
|
|
theMap[n1] = 5;
|
|
|
|
theMap[n2] = 1;
|
|
|
|
|
|
|
|
REQUIRE_EQ(2, theMap.size());
|
|
|
|
}
|
|
|
|
|
2022-10-22 01:54:01 +08:00
|
|
|
TEST_CASE("equality_of_empty_symbols")
|
|
|
|
{
|
2023-12-02 15:46:57 +08:00
|
|
|
ScopedFastFlag sff{FFlag::DebugLuauDeferredConstraintResolution, true};
|
2022-10-22 01:54:01 +08:00
|
|
|
|
|
|
|
std::string s1 = "name";
|
|
|
|
std::string s2 = "name";
|
|
|
|
|
|
|
|
AstName one{s1.data()};
|
|
|
|
AstLocal two{AstName{s2.data()}, Location(), nullptr, 0, 0, nullptr};
|
|
|
|
|
|
|
|
Symbol global{one};
|
|
|
|
Symbol local{&two};
|
|
|
|
Symbol empty1{};
|
|
|
|
Symbol empty2{};
|
|
|
|
|
|
|
|
CHECK(empty1 != global);
|
|
|
|
CHECK(empty1 != local);
|
|
|
|
CHECK(empty1 == empty2);
|
|
|
|
}
|
|
|
|
|
2021-10-30 04:25:12 +08:00
|
|
|
TEST_SUITE_END();
|