Files
swift-mirror/validation-test/stdlib/FixedPoint.swift.gyb
Dmitri Hrybenko 1eea220932 Use one module cache directory for all the lit tests to speed them up
Doing so is safe even though we have mock SDK.  The include paths for
modules with the same name in the real and mock SDKs are different, and
the module files will be distinct (because they will have a different
hash).

This reduces test runtime on OS X by 30% and brings it under a minute on
a 16-core machine.

This also uncovered some problems with some tests -- even when run for
iOS configurations, some tests would still run with macosx triple.  I
fixed the tests where I noticed this issue.

rdar://problem/19125022

Swift SVN r23683
2014-12-04 11:21:48 +00:00

272 lines
6.5 KiB
Plaintext

// RUN: rm -rf %t && mkdir -p %t && %S/../../utils/gyb %s -o %t/FixedPoint.swift
// RUN: %S/../../utils/line-directive %t/FixedPoint.swift -- %target-build-swift %t/FixedPoint.swift -o %t/a.out
// RUN: %target-run %t/a.out
import StdlibUnittest
var FixedPoint = TestSuite("FixedPoint")
%{
import gyb
test_bit_patterns = [
0x0000000000000000,
0x0000000000000001,
0xffffffffffffffff,
0x00ffffffffffffff,
0x0000ffffffffffff,
0x000000ffffffffff,
0x00000000ffffffff,
0x0000000000ffffff,
0x000000000000ffff,
0x00000000000000ff,
0xfe123456789abcde,
0x00fe123456789abc,
0x0000fe123456789a,
0x000000fe12345678,
0x00000000fe123456,
0x0000000000fe1234,
0x000000000000fe12,
0x00000000000000fe,
0x7f123456789abcde,
0x007f123456789abc,
0x00007f123456789a,
0x0000007f12345678,
0x000000007f123456,
0x00000000007f1234,
0x0000000000007f12,
0x000000000000007f,
]
def prepare_bit_pattern(bit_pattern, dst_bits, dst_signed):
mask = ((1 << dst_bits) - 1)
dst = bit_pattern & mask
if not dst_signed:
return dst
if dst <= ((1 << (dst_bits - 1)) - 1):
return dst
return dst - mask - 1
def get_fixed_point_hash(bit_pattern, src_bits, word_bits):
if src_bits <= word_bits:
bit_pattern = prepare_bit_pattern(bit_pattern, src_bits, True)
return prepare_bit_pattern(bit_pattern, word_bits, True)
if src_bits == word_bits * 2:
return prepare_bit_pattern(
bit_pattern ^ (bit_pattern >> 32), word_bits, True)
}%
//===----------------------------------------------------------------------===//
// 'Int(truncatingBitPattern:)' initializer
//===----------------------------------------------------------------------===//
%{
truncating_bit_pattern_test_template = gyb.parseTemplate("truncating_bit_pattern",
"""
% from SwiftIntTypes import *
% for dst_ty in all_integer_types(word_bits):
% Dst = dst_ty.stdlib_name
FixedPoint.test("${Dst}(truncatingBitPattern:)") {
% for src_ty in all_integer_types(word_bits):
% Src = src_ty.stdlib_name
% if should_define_truncating_bit_pattern_init(src_ty=src_ty, dst_ty=dst_ty):
%
% for bit_pattern in test_bit_patterns:
if true {
% input = prepare_bit_pattern(bit_pattern, src_ty.bits, src_ty.is_signed)
let input = get${Src}(${input})
% input = prepare_bit_pattern(input, src_ty.bits, False)
let output = get${Dst}(${Dst}(truncatingBitPattern: input))
let expected = get${Dst}(${prepare_bit_pattern(input, dst_ty.bits, dst_ty.is_signed)})
expectEqual(expected, output)
}
% end
// This comment prevents gyb from miscompiling this file.
// <rdar://problem/17548877> gyb miscompiles a certain for loop
% end
// This comment prevents gyb from miscompiling this file.
// <rdar://problem/17548877> gyb miscompiles a certain for loop
% end
}
% end
""")
}%
#if arch(i386) || arch(arm)
${gyb.executeTemplate(
truncating_bit_pattern_test_template,
prepare_bit_pattern=prepare_bit_pattern,
test_bit_patterns=test_bit_patterns,
word_bits=32)}
#elseif arch(x86_64) || arch(arm64)
${gyb.executeTemplate(
truncating_bit_pattern_test_template,
prepare_bit_pattern=prepare_bit_pattern,
test_bit_patterns=test_bit_patterns,
word_bits=64)}
#else
_UnimplementedError()
#endif
//===----------------------------------------------------------------------===//
// 'Int(bitPattern:)' initializer
//===----------------------------------------------------------------------===//
%{
bit_pattern_test_template = gyb.parseTemplate("bit_pattern",
"""
% from SwiftIntTypes import *
% for dst_ty in all_integer_types(word_bits):
% Dst = dst_ty.stdlib_name
% # Source is the same as destination, but of different signedness.
% src_ty = dst_ty.get_opposite_signedness()
% Src = src_ty.stdlib_name
FixedPoint.test("${Dst}(bitPattern: ${Src})") {
% for bit_pattern in test_bit_patterns:
if true {
% input = prepare_bit_pattern(bit_pattern, src_ty.bits, src_ty.is_signed)
let input = get${Src}(${input})
% input = prepare_bit_pattern(input, src_ty.bits, False)
let output = get${Dst}(${Dst}(bitPattern: input))
let expected = get${Dst}(${prepare_bit_pattern(input, dst_ty.bits, dst_ty.is_signed)})
expectEqual(expected, output)
}
if true {
% input = prepare_bit_pattern(bit_pattern, src_ty.bits, src_ty.is_signed)
let input = get${Src}(${input})
// Pass a literal directly.
let literalOutput = get${Dst}(${Dst}(bitPattern: ${input}))
let output = get${Dst}(${Dst}(bitPattern: input))
% input = prepare_bit_pattern(input, src_ty.bits, False)
let expected = get${Dst}(${prepare_bit_pattern(input, dst_ty.bits, dst_ty.is_signed)})
expectEqual(expected, literalOutput)
expectEqual(expected, output)
}
% end
}
% end
""")
}%
#if arch(i386) || arch(arm)
${gyb.executeTemplate(
bit_pattern_test_template,
prepare_bit_pattern=prepare_bit_pattern,
test_bit_patterns=test_bit_patterns,
word_bits=32)}
#elseif arch(x86_64) || arch(arm64)
${gyb.executeTemplate(
bit_pattern_test_template,
prepare_bit_pattern=prepare_bit_pattern,
test_bit_patterns=test_bit_patterns,
word_bits=64)}
#else
_UnimplementedError()
#endif
//===----------------------------------------------------------------------===//
// 'Int.hashValue' property
//===----------------------------------------------------------------------===//
%{
hash_value_test_template = gyb.parseTemplate("hash_value",
"""
% from SwiftIntTypes import *
% for self_ty in all_integer_types(word_bits):
% Self = self_ty.stdlib_name
FixedPoint.test("${Self}.hashValue") {
% for bit_pattern in test_bit_patterns:
if true {
% input = prepare_bit_pattern(bit_pattern, self_ty.bits, self_ty.is_signed)
let input = get${Self}(${input})
let output = getInt(input.hashValue)
let expected = getInt(${get_fixed_point_hash(input, self_ty.bits, word_bits)})
expectEqual(expected, output)
}
% end
}
% end
""")
}%
#if arch(i386) || arch(arm)
${gyb.executeTemplate(
hash_value_test_template,
prepare_bit_pattern=prepare_bit_pattern,
get_fixed_point_hash=get_fixed_point_hash,
test_bit_patterns=test_bit_patterns,
word_bits=32)}
#elseif arch(x86_64) || arch(arm64)
${gyb.executeTemplate(
hash_value_test_template,
prepare_bit_pattern=prepare_bit_pattern,
get_fixed_point_hash=get_fixed_point_hash,
test_bit_patterns=test_bit_patterns,
word_bits=64)}
#else
_UnimplementedError()
#endif
runAllTests()