Skip to content

Commit

Permalink
Migrate tests to absl.testing, mainly to make Bazel test sharding work.
Browse files Browse the repository at this point in the history
  • Loading branch information
poletti-marco committed Jul 21, 2019
1 parent 9d6c3af commit 4ded6e2
Show file tree
Hide file tree
Showing 49 changed files with 12,309 additions and 12,349 deletions.
7 changes: 1 addition & 6 deletions tests/build_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,5 @@ def fruit_py_tests(srcs, data=[]):
"//third_party/fruit/tests:libfruit.so",
"//third_party/fruit/tests:test_headers_filegroup",
],
args = [
"-p",
"no:cacheprovider",
"-n",
"4",
],
shard_count = 32,
)
242 changes: 122 additions & 120 deletions tests/data_structures/test_fixed_size_allocator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from absl.testing import parameterized
from fruit_test_common import *

COMMON_DEFINITIONS = '''
Expand Down Expand Up @@ -64,130 +65,131 @@
};
'''

def test_empty_allocator():
source = '''
int main() {
FixedSizeAllocator allocator;
}
'''
expect_success(
COMMON_DEFINITIONS,
source,
locals())
class TestFixedSizeAllocator(parameterized.TestCase):
def test_empty_allocator(self):
source = '''
int main() {
FixedSizeAllocator allocator;
}
'''
expect_success(
COMMON_DEFINITIONS,
source,
locals())

def test_2_types():
source = '''
int main() {
{
FixedSizeAllocator::FixedSizeAllocatorData allocator_data;
allocator_data.addType(getTypeId<X>());
allocator_data.addType(getTypeId<Y>());
FixedSizeAllocator allocator(allocator_data);
allocator.constructObject<X>(15);
allocator.constructObject<Y>();
Assert(X::num_instances == 1);
Assert(Y::num_instances == 1);
}
Assert(X::num_instances == 0);
Assert(Y::num_instances == 0);
}
'''
expect_success(
COMMON_DEFINITIONS,
source,
locals())
def test_2_types(self):
source = '''
int main() {
{
FixedSizeAllocator::FixedSizeAllocatorData allocator_data;
allocator_data.addType(getTypeId<X>());
allocator_data.addType(getTypeId<Y>());
FixedSizeAllocator allocator(allocator_data);
allocator.constructObject<X>(15);
allocator.constructObject<Y>();
Assert(X::num_instances == 1);
Assert(Y::num_instances == 1);
}
Assert(X::num_instances == 0);
Assert(Y::num_instances == 0);
}
'''
expect_success(
COMMON_DEFINITIONS,
source,
locals())

def test_externally_allocated_only():
source = '''
int main() {
{
FixedSizeAllocator::FixedSizeAllocatorData allocator_data;
allocator_data.addExternallyAllocatedType(getTypeId<X>());
FixedSizeAllocator allocator(allocator_data);
allocator.registerExternallyAllocatedObject(new X(15));
// The allocator takes ownership. Valgrind will report an error if X is not deleted.
Assert(X::num_instances == 1);
}
Assert(X::num_instances == 0);
}
'''
expect_success(
COMMON_DEFINITIONS,
source,
locals())
def test_externally_allocated_only(self):
source = '''
int main() {
{
FixedSizeAllocator::FixedSizeAllocatorData allocator_data;
allocator_data.addExternallyAllocatedType(getTypeId<X>());
FixedSizeAllocator allocator(allocator_data);
allocator.registerExternallyAllocatedObject(new X(15));
// The allocator takes ownership. Valgrind will report an error if X is not deleted.
Assert(X::num_instances == 1);
}
Assert(X::num_instances == 0);
}
'''
expect_success(
COMMON_DEFINITIONS,
source,
locals())

def test_mix():
source = '''
int main() {
{
FixedSizeAllocator::FixedSizeAllocatorData allocator_data;
allocator_data.addExternallyAllocatedType(getTypeId<X>());
allocator_data.addType(getTypeId<Y>());
FixedSizeAllocator allocator(allocator_data);
allocator.registerExternallyAllocatedObject(new X(15));
// The allocator takes ownership. Valgrind will report an error if X is not deleted.
allocator.constructObject<Y>();
Assert(X::num_instances == 1);
Assert(Y::num_instances == 1);
}
Assert(X::num_instances == 0);
Assert(Y::num_instances == 0);
}
'''
expect_success(
COMMON_DEFINITIONS,
source,
locals())
def test_mix(self):
source = '''
int main() {
{
FixedSizeAllocator::FixedSizeAllocatorData allocator_data;
allocator_data.addExternallyAllocatedType(getTypeId<X>());
allocator_data.addType(getTypeId<Y>());
FixedSizeAllocator allocator(allocator_data);
allocator.registerExternallyAllocatedObject(new X(15));
// The allocator takes ownership. Valgrind will report an error if X is not deleted.
allocator.constructObject<Y>();
Assert(X::num_instances == 1);
Assert(Y::num_instances == 1);
}
Assert(X::num_instances == 0);
Assert(Y::num_instances == 0);
}
'''
expect_success(
COMMON_DEFINITIONS,
source,
locals())

def test_alignment():
source = '''
int main() {
FixedSizeAllocator::FixedSizeAllocatorData allocator_data;
allocator_data.addType(getTypeId<TypeWithAlignment<1>>());
allocator_data.addType(getTypeId<TypeWithAlignment<8>>());
allocator_data.addType(getTypeId<TypeWithAlignment<2>>());
allocator_data.addType(getTypeId<TypeWithAlignment<128>>());
allocator_data.addType(getTypeId<TypeWithAlignment<2>>());
allocator_data.addType(getTypeId<TypeWithAlignment<8>>());
allocator_data.addType(getTypeId<TypeWithAlignment<1>>());
FixedSizeAllocator allocator(allocator_data);
// TypeWithLargeAlignment::TypeWithLargeAlignment() will assert that the alignment is correct.
allocator.constructObject<TypeWithAlignment<2>>();
allocator.constructObject<TypeWithAlignment<8>>();
allocator.constructObject<TypeWithAlignment<1>>();
allocator.constructObject<TypeWithAlignment<128>>();
allocator.constructObject<TypeWithAlignment<1>>();
allocator.constructObject<TypeWithAlignment<8>>();
allocator.constructObject<TypeWithAlignment<2>>();
}
'''
expect_success(
COMMON_DEFINITIONS,
source,
locals())
def test_alignment(self):
source = '''
int main() {
FixedSizeAllocator::FixedSizeAllocatorData allocator_data;
allocator_data.addType(getTypeId<TypeWithAlignment<1>>());
allocator_data.addType(getTypeId<TypeWithAlignment<8>>());
allocator_data.addType(getTypeId<TypeWithAlignment<2>>());
allocator_data.addType(getTypeId<TypeWithAlignment<128>>());
allocator_data.addType(getTypeId<TypeWithAlignment<2>>());
allocator_data.addType(getTypeId<TypeWithAlignment<8>>());
allocator_data.addType(getTypeId<TypeWithAlignment<1>>());
FixedSizeAllocator allocator(allocator_data);
// TypeWithLargeAlignment::TypeWithLargeAlignment() will assert that the alignment is correct.
allocator.constructObject<TypeWithAlignment<2>>();
allocator.constructObject<TypeWithAlignment<8>>();
allocator.constructObject<TypeWithAlignment<1>>();
allocator.constructObject<TypeWithAlignment<128>>();
allocator.constructObject<TypeWithAlignment<1>>();
allocator.constructObject<TypeWithAlignment<8>>();
allocator.constructObject<TypeWithAlignment<2>>();
}
'''
expect_success(
COMMON_DEFINITIONS,
source,
locals())

def test_move_constructor():
source = '''
int main() {
{
FixedSizeAllocator::FixedSizeAllocatorData allocator_data;
allocator_data.addType(getTypeId<X>());
allocator_data.addType(getTypeId<Y>());
FixedSizeAllocator allocator(allocator_data);
allocator.constructObject<X>(15);
FixedSizeAllocator allocator2(std::move(allocator));
allocator2.constructObject<Y>();
Assert(X::num_instances == 1);
Assert(Y::num_instances == 1);
}
Assert(X::num_instances == 0);
Assert(Y::num_instances == 0);
}
'''
expect_success(
COMMON_DEFINITIONS,
source,
locals())
def test_move_constructor(self):
source = '''
int main() {
{
FixedSizeAllocator::FixedSizeAllocatorData allocator_data;
allocator_data.addType(getTypeId<X>());
allocator_data.addType(getTypeId<Y>());
FixedSizeAllocator allocator(allocator_data);
allocator.constructObject<X>(15);
FixedSizeAllocator allocator2(std::move(allocator));
allocator2.constructObject<Y>();
Assert(X::num_instances == 1);
Assert(Y::num_instances == 1);
}
Assert(X::num_instances == 0);
Assert(Y::num_instances == 0);
}
'''
expect_success(
COMMON_DEFINITIONS,
source,
locals())

if __name__ == '__main__':
main(__file__)
absltest.main()
Loading

0 comments on commit 4ded6e2

Please sign in to comment.