CodeSampleX

샘플

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 rejecting…

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

컨트랙트

  1. 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
  2. assert Set#<=> returns -1, 0, or 1 only when sets have a subset, equality, or superset relationship
  3. assert relational operators <, <=, >, >= on disjoint sets evaluate to false in both directions rather than establishing an ordering
  4. 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
  5. assert Set#<=> returns nil when comparing a Set with an Array

파일

  • Gemfile
  • NOTES.md
  • csx.json
  • test/contract.rb

소스 아티팩트 내려받기 (tar.gz)

소스

Gemfile
source "https://rubygems.org"

gem "set", "1.1.1"
gem "minitest", "5.27.0"
NOTES.md
# 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`.
csx.json
{"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"}
test/contract.rb
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

오리진 시더

csx-seed