Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 8a78514

Browse files
committed
[[ Foundation ]] Index name tests
This patch adds some c++ tests for the index names feature / optimisation
1 parent 88a2ff6 commit 8a78514

File tree

3 files changed

+134
-2
lines changed

3 files changed

+134
-2
lines changed

libfoundation/libfoundation.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
'test/test_foreign.cpp',
1818
'test/test_hash.cpp',
1919
'test/test_memory.cpp',
20+
'test/test_name.cpp',
2021
'test/test_proper-list.cpp',
2122
'test/test_string.cpp',
2223
'test/test_typeconvert.cpp',

libfoundation/test/test_hash.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,18 @@ MCAutoStringRef string_create_utf8(const char (&utf8)[N])
137137
return s;
138138
}
139139

140+
template<size_t N>
141+
MCAutoStringRef string_create_native(const char (&native)[N])
142+
{
143+
MCAutoStringRef s;
144+
MCStringCreateWithNativeChars((const char_t *)native, N-1, &s);
145+
return s;
146+
}
147+
140148
TEST(hash, native_string)
141149
{
142-
MCAutoStringRef t_mixed = string_create_utf8(u8"LiveCode");
143-
MCAutoStringRef t_lower = string_create_utf8(u8"livecode");
150+
MCAutoStringRef t_mixed = string_create_native("LiveCode");
151+
MCAutoStringRef t_lower = string_create_native("livecode");
144152

145153
/* Check a couple of specific values */
146154
EXPECT_EQ(string_hash_exact(kMCEmptyString), {2166136261});

libfoundation/test/test_name.cpp

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/* Copyright (C) 2003-2015 LiveCode Ltd.
2+
3+
This file is part of LiveCode.
4+
5+
LiveCode is free software; you can redistribute it and/or modify it under
6+
the terms of the GNU General Public License v3 as published by the Free
7+
Software Foundation.
8+
9+
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
10+
WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
16+
17+
#include "gtest/gtest.h"
18+
19+
#include "foundation.h"
20+
#include "foundation-auto.h"
21+
22+
TEST(name, index_equal_string)
23+
{
24+
static index_t s_test_indicies[] =
25+
{
26+
INT32_MIN + 32,
27+
INT16_MIN,
28+
INT8_MIN,
29+
0,
30+
INT8_MAX,
31+
INT16_MAX,
32+
INT32_MAX - 32,
33+
};
34+
for(size_t i = 0; i < sizeof(s_test_indicies) / sizeof(s_test_indicies[0]); i++)
35+
{
36+
for(int j = -32; j <= 32; j++)
37+
{
38+
int64_t t_full_index = s_test_indicies[i] + j;
39+
index_t t_index = (index_t)t_full_index;
40+
char_t t_index_str[32];
41+
sprintf((char *)t_index_str, "%lld", t_full_index);
42+
43+
// fprintf(stderr, "%lld, %d\n", t_full_index, t_index);
44+
45+
MCNewAutoNameRef t_index_name, t_string_name;
46+
ASSERT_TRUE(MCNameCreateWithIndex(t_index, &t_index_name));
47+
ASSERT_TRUE(MCNameCreateWithNativeChars(t_index_str, strlen((char *)t_index_str), &t_string_name));
48+
// fprintf(stderr, "%s, %s\n", MCStringGetCString(MCNameGetString(*t_index_name)), MCStringGetCString(MCNameGetString(*t_string_name)));
49+
ASSERT_EQ(*t_index_name, *t_string_name);
50+
}
51+
}
52+
}
53+
54+
TEST(name, distinct_string)
55+
{
56+
static constexpr const char *s_test_names[] =
57+
{
58+
"foo",
59+
"bar",
60+
"baz"
61+
"foobar",
62+
"foobaz",
63+
"foobarbaz",
64+
};
65+
static constexpr size_t s_test_name_count = sizeof(s_test_names) / sizeof(s_test_names[0]);
66+
67+
MCNewAutoNameRef t_names[s_test_name_count];
68+
for(size_t i = 0; i < s_test_name_count; i++)
69+
{
70+
ASSERT_TRUE(MCNameCreateWithNativeChars((const char_t *)s_test_names[i], strlen(s_test_names[i]), &t_names[i]));
71+
for(size_t j = 0; j < i; j++)
72+
ASSERT_NE(*t_names[i], *t_names[j]);
73+
}
74+
}
75+
76+
TEST(name, equal_string)
77+
{
78+
static constexpr const char *s_test_names[] =
79+
{
80+
"foo",
81+
"foo",
82+
"baz",
83+
"baz",
84+
"foobaz",
85+
"foobaz",
86+
};
87+
static constexpr size_t s_test_name_count = sizeof(s_test_names) / sizeof(s_test_names[0]);
88+
89+
MCNewAutoNameRef t_names[s_test_name_count];
90+
for(size_t i = 0; i < s_test_name_count; i++)
91+
{
92+
ASSERT_TRUE(MCNameCreateWithNativeChars((const char_t *)s_test_names[i], strlen(s_test_names[i]), &t_names[i]));
93+
}
94+
for(size_t i = 0; i < s_test_name_count / 2; i++)
95+
{
96+
ASSERT_EQ(*t_names[i * 2], *t_names[i * 2 + 1]);
97+
}
98+
}
99+
100+
TEST(name, equiv_string)
101+
{
102+
static constexpr const char *s_test_names[] =
103+
{
104+
"foo",
105+
"FoO",
106+
"baz",
107+
"baZ",
108+
"fooBaz",
109+
"Foobaz",
110+
};
111+
static constexpr size_t s_test_name_count = sizeof(s_test_names) / sizeof(s_test_names[0]);
112+
113+
MCNewAutoNameRef t_names[s_test_name_count];
114+
for(size_t i = 0; i < s_test_name_count; i++)
115+
{
116+
ASSERT_TRUE(MCNameCreateWithNativeChars((const char_t *)s_test_names[i], strlen(s_test_names[i]), &t_names[i]));
117+
}
118+
for(size_t i = 0; i < s_test_name_count / 2; i++)
119+
{
120+
fprintf(stderr, "%s, %s\n", MCStringGetCString(MCNameGetString(*t_names[i * 2])), MCStringGetCString(MCNameGetString(*t_names[i * 2 + 1])));
121+
ASSERT_TRUE(MCNameIsEqualToCaseless(*t_names[i * 2], *t_names[i * 2 + 1]));
122+
}
123+
}

0 commit comments

Comments
 (0)