Skip to content

Adding validation tests for LazyFilterCollection and LazyMapCollection #2247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions validation-test/stdlib/Collection/LazyFilterCollection.swift.gyb
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// -*- swift -*-

// RUN: rm -rf %t
// RUN: mkdir -p %t
// RUN: %S/../../../utils/gyb %s -o %t/main.swift
// RUN: %S/../../../utils/line-directive %t/main.swift -- %target-build-swift %t/main.swift -o %t/LazyFilterCollection.swift.a.out
// RUN: %S/../../../utils/line-directive %t/main.swift -- %target-run %t/LazyFilterCollection.swift.a.out
// REQUIRES: executable_test

import StdlibUnittest
import StdlibCollectionUnittest

var CollectionTests = TestSuite("Collection")

%{
variations = [('', 'Sequence'), ('', 'Collection'), ('Bidirectional', 'Collection')]
}%

// Test collections using value types as elements.
% for (traversal, kind) in variations:
CollectionTests.add${traversal}${kind}Tests(
make${kind}: { (elements: [OpaqueValue<Int>]) -> LazyFilter${kind}<Minimal${traversal}${kind}<OpaqueValue<Int>>> in
// FIXME: create a better sequence and filter
Minimal${traversal}${kind}(elements: elements).lazy.filter { _ in return true }
},
wrapValue: identity,
extractValue: identity,
make${kind}OfEquatable: { (elements: [MinimalEquatableValue]) -> LazyFilter${kind}<Minimal${traversal}${kind}<MinimalEquatableValue>> in
// FIXME: create a better sequence and filter
Minimal${traversal}${kind}(elements: elements).lazy.filter { _ in return true }
},
wrapValueIntoEquatable: identityEq,
extractValueFromEquatable: identityEq
)
% end

// Test collections using reference types as elements.
% for (traversal, kind) in variations:
CollectionTests.add${traversal}${kind}Tests(
make${kind}: { (elements: [LifetimeTracked]) -> LazyFilter${kind}<Minimal${traversal}${kind}<LifetimeTracked]>> in
// FIXME: create a better sequence and filter
Minimal${traversal}${kind}(elements: elements).lazy.filter { _ in return true }
},
wrapValue: { (element: OpaqueValue<Int>) in
LifetimeTracked(element.value, identity: element.identity)
},
extractValue: { (element: LifetimeTracked) in
OpaqueValue(element.value, identity: element.identity)
},
make${kind}OfEquatable: { (elements: [LifetimeTracked]) -> LazyFilter${kind}<Minimal${traversal}${kind}<LifetimeTracked>> in
// FIXME: create a better sequence and filter
Minimal${traversal}${kind}(elements: elements).lazy.filter { _ in return true }
},
wrapValueIntoEquatable: { (element: MinimalEquatableValue) in
LifetimeTracked(element.value, identity: element.identity)
},
extractValueFromEquatable: { (element: LifetimeTracked) in
MinimalEquatableValue(element.value, identity: element.identity)
}
)
% end

// Test collection instances and iterators.
CollectionTests.test("LazyFilterCollection instances") {
% for (traversal, kind) in variations:
do {
let expected : [String] = []
let base = ["apple", "orange", "banana", "grapefruit", "lychee"]
% if kind == 'Sequence':
checkSequence(expected, MinimalSequence(elements: base).lazy.filter { _ in return false })
% elif traversal == '' and kind == 'Collection':
checkForwardCollection(expected, MinimalCollection(elements: base).lazy.filter { _ in return false }, sameValue: { $0 == $1 })
% else:
check${traversal}${kind}(expected, Minimal${traversal}${kind}(elements: base).lazy.filter { _ in return false }, sameValue: { $0 == $1 })
% end
}
do {
let expected = ["apple", "orange", "banana", "grapefruit", "lychee"]
let base = ["apple", "orange", "banana", "grapefruit", "lychee"]
% if kind == 'Sequence':
checkSequence(expected, MinimalSequence(elements: base).lazy.filter { _ in return true })
% elif traversal == '' and kind == 'Collection':
checkForwardCollection(expected, MinimalCollection(elements: base).lazy.filter { _ in return true }, sameValue: { $0 == $1 })
% else:
check${traversal}${kind}(expected, Minimal${traversal}${kind}(elements: base).lazy.filter { _ in return true }, sameValue: { $0 == $1 })
% end
}
do {
let expected = [2, 4, 6, 8, 10, 12, 14, 16]
let base = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
% if kind == 'Sequence':
checkSequence(expected, MinimalSequence(elements: base).lazy.filter { $0 % 2 == 0 })
% elif traversal == '' and kind == 'Collection':
checkForwardCollection(expected, MinimalCollection(elements: base).lazy.filter { $0 % 2 == 0 }, sameValue: { $0 == $1 })
% else:
check${traversal}${kind}(expected, Minimal${traversal}${kind}(elements: base).lazy.filter { $0 % 2 == 0 }, sameValue: { $0 == $1 })
}
% end
}

runAllTests()
87 changes: 87 additions & 0 deletions validation-test/stdlib/Collection/LazyMapCollection.swift.gyb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// -*- swift -*-

// RUN: rm -rf %t
// RUN: mkdir -p %t
// RUN: %S/../../../utils/gyb %s -o %t/main.swift
// RUN: %S/../../../utils/line-directive %t/main.swift -- %target-build-swift %t/main.swift -o %t/LazyMapCollection.swift.a.out
// RUN: %S/../../../utils/line-directive %t/main.swift -- %target-run %t/LazyMapCollection.swift.a.out
// REQUIRES: executable_test

import StdlibUnittest
import StdlibCollectionUnittest

var CollectionTests = TestSuite("Collection")

%{
variations = [('', 'Sequence'), ('', 'Collection'), ('Bidirectional', 'Collection'), ('RandomAccess', 'Collection')]
}%

// Test collections using value types as elements.
% for (traversal, kind) in variations:
CollectionTests.add${traversal}${kind}Tests(
make${kind}: { (elements: [OpaqueValue<Int>]) -> LazyMap${traversal}${kind}<Minimal${traversal}${kind}<OpaqueValue<Int>>, OpaqueValue<Int>> in
Minimal${traversal}${kind}(elements: elements).lazy.map(identity)
},
wrapValue: identity,
extractValue: identity,
make${kind}OfEquatable: { (elements: [MinimalEquatableValue]) -> LazyMap${traversal}${kind}<Minimal${traversal}${kind}<MinimalEquatableValue>, MinimalEquatableValue> in
Minimal${traversal}${kind}(elements: elements).lazy.map(identityEq)
},
wrapValueIntoEquatable: identityEq,
extractValueFromEquatable: identityEq
)
% end

// Test collections using reference types as elements.
% for (traversal, kind) in variations:
CollectionTests.add${traversal}${kind}Tests(
make${kind}: { (elements: [LifetimeTracked]) -> LazyMap${traversal}${kind}<Minimal${traversal}${kind}<LifetimeTracked>, LifetimeTracked> in
Minimal${traversal}${kind}(elements: elements).lazy.map { $0 }
},
wrapValue: { (element: OpaqueValue<Int>) in
LifetimeTracked(element.value, identity: element.identity)
},
extractValue: { (element: LifetimeTracked) in
OpaqueValue(element.value, identity: element.identity)
},
make${kind}OfEquatable: { (elements: [LifetimeTracked]) -> LazyMap${traversal}${kind}<Minimal${traversal}${kind}<LifetimeTracked>, LifetimeTracked> in
Minimal${traversal}${kind}(elements: elements).lazy.map { $0 }
},
wrapValueIntoEquatable: { (element: MinimalEquatableValue) in
LifetimeTracked(element.value, identity: element.identity)
},
extractValueFromEquatable: { (element: LifetimeTracked) in
MinimalEquatableValue(element.value, identity: element.identity)
}
)
% end

// Test sequence instances and iterators.
CollectionTests.test("LazyMapCollection instances") {
% for (traversal, kind) in variations:
do {
let expected = ["convent", "conform", "constrict", "condone"]
let base = ["vent", "form", "strict", "done"]
% if kind == 'Sequence':
checkSequence(expected, MinimalSequence(elements: base).lazy.map { "con" + $0 })
% elif traversal == '' and kind == 'Collection':
checkForwardCollection(expected, MinimalCollection(elements: base).lazy.map { "con" + $0 }, sameValue: { $0 == $1 })
% else:
check${traversal}${kind}(expected, Minimal${traversal}${kind}(elements: base).lazy.map { "con" + $0 }, sameValue: { $0 == $1 })
% end
}
do {
let expected = [1, 4, 9, 16, 25, 36, 49, 64]
let base = [1, 2, 3, 4, 5, 6, 7, 8]
% if kind == 'Sequence':
checkSequence(expected, MinimalSequence(elements: base).lazy.map { $0 * $0 })
% elif traversal == '' and kind == 'Collection':
checkForwardCollection(expected, MinimalCollection(elements: base).lazy.map { $0 * $0 }, sameValue: { $0 == $1 })
% else:
check${traversal}${kind}(expected, Minimal${traversal}${kind}(elements: base).lazy.map { $0 * $0 }, sameValue: { $0 == $1 })
% end
}
% end
}

runAllTests()