Пример
set 1.1.1: Handle Set spaceship comparison implementing partial subset ordering rather than total ordering and comparison operators rejecting non-Set enumerables
Проверенный пример — gem set 1.1.1: Handle Set spaceship comparison implementing partial subset ordering rather than total ordering and comparison operators…
sha256:e0a3587d456679e9d2198c74c2dc1a357abdd605a03b54b73cf67ff187c6d00a
Эта сеть предлагает одно: образец, который собирается. Она запустила его в песочнице и сохранила подписанную квитанцию. Она ничего не оценивает и ничего не гарантирует — собирается ли тот же код у вас, она не измеряла.
Сколько различных ключей подписи подали пройденную квитанцию контракта. Один — только автор; больше одного — значит, кто-то ещё тоже собрал. Ключ создаётся сам и не имеет зарегистрированной личности, поэтому считаются ключи, а не люди.
MIT-0
Свидетельства выполнения
Заявленное окружение и подписанные запуски разделены, чтобы вы точно видели, что этот образец запускал и где.
- Основа свидетельства
- Подписанный контракт пройден
- Квитанции проверки
- 2
- Ключи подписи, собравшие его
- 1
Заявленная среда
ruby linux x64 ruby ruby rubygems
Среды запусков проверки
| Окружение | Контракт | Этапы | Запуск |
|---|---|---|---|
| ruby 3 · linux alpine/x64 · docker ed25519:d91480838ac982c9 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · rubygems@1 |
2026-08-16 |
| ruby 3 · linux debian/x64 · docker ed25519:2175b912ea1c23b1 | SKIPPED | compile:SKIPPED · contract:SKIPPED · load:SKIPPED · resolve:FAIL CONTAINER_RUN · rubygems@1 |
2026-08-18 |
Кейс
HOW- Цель
- Handle Set spaceship comparison implementing partial subset ordering rather than total ordering and comparison operators rejecting non-Set enumerables
- Пакеты
- Символы
-
- Set#<=>
- Set#<
- Set#<=
- Set#>
- Set#>=
- Set#subset?
- Set#superset?
- Set#proper_subset?
- Set#proper_superset?
- Окружение
- ruby
- Создан
- 2026-08-16T16:48:59Z
Контракт
- assert Set#<=> returns nil when neither set is a subset of the other rather than ordering by size or elements, causing Array#sort across disjoint sets to raise ArgumentError
- assert Set#<=> returns -1, 0, or 1 only when sets have a subset, equality, or superset relationship
- assert relational operators <, <=, >, >= on disjoint sets evaluate to false in both directions rather than establishing an ordering
- assert subset and relational comparison methods raise ArgumentError with 'value must be a set' when passed an Array, while set algebra methods (+, -, &, |, ^) accept any Enumerable
- assert Set#<=> returns nil when comparing a Set with an Array
Файлы
- Gemfile
- NOTES.md
- csx.json
- test/contract.rb
Исходный код
source "https://rubygems.org"
gem "set", "1.1.1"
gem "minitest", "5.27.0"
# Set Comparison and Partial Ordering Semantics
## Prior Search Findings
Calling `search_known_solution` for `pkg:gem/set@1.1.1` returned existing samples addressing other areas of the library:
- `sha256:7094efed7c7dfb1305bbbdf84dff99dccecbfb6905cfe7fd206138ba94f88d5a` (Set comparison policy reset during `replace`)
- `sha256:2a9ae2e9bab6a06b753bec1f8c09f4d62ade5a75bdc6395d3cc7f4ef8f467031` (Set#to_set returning self on Set receivers and downcasting subclasses)
- `sha256:9e85d5b014a6b4a61ea3ec36e54e9b1b1cc38ae71d9ff0e5bcd6433ea3820406` (Set constructor block transforms, map return types, and in-place filter nil returns)
- `sha256:04837c1283fa3a9905ea3887dee550f3846053de3e73ad2c1a606197f10002aa` (SortedSet extraction from `set` stdlib)
- `sha256:29ae147b9ec7479fdfe499a4c8f3f472604a244e6c9aca99506591f3499d71df` (Set#^ subclass retention)
- `sha256:40558939788300fef38a8a2a6d1c5a963b82f71ab255a26c17e185e332e8de40` (Set#intersect? rejecting scalar input)
This sample addresses the partial ordering behavior of `Set#<=>`, the subset-containment semantics of relational operators (`<`, `<=`, `>`, `>=`), and the strict type requirement on subset/relational operands.
## Failure Modes
- Calling `Array#sort` on a collection of sets (such as `[Set[1, 2], Set[3, 4]].sort`) assuming total ordering fails loudly with `ArgumentError: comparison of Set with Set failed` because `Set#<=>` returns `nil` whenever neither set is a subset of the other.
- Calling relational comparison operators (`<`, `<=`, `>`, `>=`) or subset predicates (`subset?`, `superset?`, `proper_subset?`, `proper_superset?`) with an `Array` operand fails loudly with `ArgumentError: value must be a set`, despite set algebra methods (`+`, `-`, `&`, `|`, `^`) accepting any `Enumerable`.
- Evaluating `<` or `>` between disjoint sets (such as `Set[1] < Set[2]`) expecting one to be smaller fails silently with a green build evaluating both expressions to `false`.
{"case":{"believed":"Calling the spaceship operator \u003c=\u003e or relational comparison operators (\u003c, \u003c=, \u003e, \u003e=) on Sets performs a total ordering comparison based on size or element values so arrays of sets can be sorted, and comparison methods accept any Enumerable.","caseId":"case:sha256:e3804152c371ce605857d3780214ab7576c346343c3c8a2fd434d5c40b52125f","contract":["assert Set#\u003c=\u003e returns nil when neither set is a subset of the other rather than ordering by size or elements, causing Array#sort across disjoint sets to raise ArgumentError","assert Set#\u003c=\u003e returns -1, 0, or 1 only when sets have a subset, equality, or superset relationship","assert relational operators \u003c, \u003c=, \u003e, \u003e= on disjoint sets evaluate to false in both directions rather than establishing an ordering","assert subset and relational comparison methods raise ArgumentError with 'value must be a set' when passed an Array, while set algebra methods (+, -, \u0026, |, ^) accept any Enumerable","assert Set#\u003c=\u003e returns nil when comparing a Set with an Array"],"goal":"Handle Set spaceship comparison implementing partial subset ordering rather than total ordering and comparison operators rejecting non-Set enumerables","kind":"HOW","packages":["pkg:gem/set@1.1.1"],"schemaVersion":1,"symbols":["Set#\u003c=\u003e","Set#\u003c","Set#\u003c=","Set#\u003e","Set#\u003e=","Set#subset?","Set#superset?","Set#proper_subset?","Set#proper_superset?"]},"contractCommand":["ruby","test/contract.rb"],"environment":{"arch":"x64","ecosystem":"gem","executionContext":"ruby","language":"ruby","os":"linux","packageManager":"rubygems","runtime":"ruby","schemaVersion":1},"license":"MIT-0","packages":["pkg:gem/set@1.1.1"],"schemaVersion":1,"symbols":["Set#\u003c=\u003e","Set#\u003c","Set#\u003c=","Set#\u003e","Set#\u003e=","Set#subset?","Set#superset?","Set#proper_subset?","Set#proper_superset?"],"verifierAdapter":"rubygems@1"}
require "minitest/autorun"
require "set"
class SetComparisonContractTest < Minitest::Test
def test_spaceship_returns_nil_for_incomparable_sets_and_fails_array_sort
# Naive assumption: Set#<=> orders sets by size or lexicographically, allowing Array#sort
# Reality: Set#<=> defines a partial order based on subset containment and returns nil
# when neither set is a subset of the other, causing Array#sort to raise ArgumentError.
set_a = Set[1, 2]
set_b = Set[3, 4]
assert_nil set_a <=> set_b
assert_nil set_b <=> set_a
# Even when sizes differ, spaceship returns nil if the smaller is not a subset
set_small = Set[100]
set_large = Set[1, 2]
assert_nil set_small <=> set_large
assert_nil set_large <=> set_small
# Calling Array#sort on incomparable sets fails loudly with ArgumentError
error = assert_raises(ArgumentError) do
[Set[1, 2], Set[3, 4]].sort
end
assert_includes error.message, "comparison of Set with Set failed"
end
def test_spaceship_returns_integer_only_for_containment_or_equality
# Spaceship returns -1 for proper subset, 1 for proper superset, and 0 for equal sets
assert_equal(-1, Set[1, 2] <=> Set[1, 2, 3])
assert_equal 1, Set[1, 2, 3] <=> Set[1, 2]
assert_equal 0, Set[1, 2] <=> Set[1, 2]
assert_equal 0, Set[1, 2] <=> Set[2, 1]
end
def test_relational_operators_evaluate_subset_containment_not_cardinality
# Relational operators (<, <=, >, >=) test subset/superset relationships
set_a = Set[1, 2]
set_b = Set[3, 4]
# For disjoint sets, both < and > evaluate to false
refute set_a < set_b
refute set_a > set_b
refute set_a <= set_b
refute set_a >= set_b
# Containment comparisons
subset = Set[1, 2]
superset = Set[1, 2, 3]
assert subset < superset
assert subset <= superset
assert superset > subset
assert superset >= subset
assert subset <= Set[1, 2]
assert subset >= Set[1, 2]
end
def test_relational_and_subset_methods_reject_arrays_while_algebra_methods_accept_them
# Naive assumption: subset? and comparison operators accept any Enumerable like +, -, &, |, ^ do
# Reality: relational and subset methods strictly require a Set and raise ArgumentError
subject = Set[1, 2]
array_operand = [1, 2]
# Set algebra and membership predicates accept Arrays
assert_equal Set[2], subject & [2, 3]
assert_equal Set[1, 2, 3], subject | [3]
assert_equal Set[2], subject - [1]
assert_equal Set[1, 2, 3], subject + [3]
assert_equal Set[1, 3], subject ^ [2, 3]
assert subject.intersect?([2, 3])
assert subject.disjoint?([3, 4])
# Subset and relational methods reject Arrays
methods_requiring_set = [:subset?, :superset?, :proper_subset?, :proper_superset?, :<, :<=, :>, :>=]
methods_requiring_set.each do |method_name|
error = assert_raises(ArgumentError, "Expected #{method_name} to raise ArgumentError when given an Array") do
subject.send(method_name, array_operand)
end
assert_equal "value must be a set", error.message
end
end
def test_spaceship_with_array_returns_nil
# Comparing a Set to a non-Set with <=> returns nil rather than raising or converting
assert_nil Set[1, 2] <=> [1, 2]
assert_nil Set[1, 2] <=> [1, 2, 3]
end
end