Class: NatSetTest

Inherits:
Test::Unit::TestCase
  • Object
show all
Defined in:
lib/natset.rb

Instance Method Summary collapse

Instance Method Details

#test_complementObject



314
315
316
317
318
319
# File 'lib/natset.rb', line 314

def test_complement
  assert_equal(NatSet.empty, ~NatSet.universal)
  assert_equal(NatSet.universal, ~NatSet.empty)
  assert_equal(NatSet._new(1, 2), ~NatSet._new(0, 1, 2))
  assert_equal(NatSet._new(0, 1, 2), ~NatSet._new(1, 2))
end

#test_each_rangeObject



402
403
404
405
406
407
408
409
# File 'lib/natset.rb', line 402

def test_each_range
  rs = []; NatSet.new() .each_range {|r| rs << r}; assert_equal([], rs)
  rs = []; NatSet.new(0).each_range {|r| rs << r}; assert_equal([0..0], rs)
  rs = []; NatSet.new(1).each_range {|r| rs << r}; assert_equal([1..1], rs)
  rs = []; NatSet.new(1..3).each_range {|r| rs << r}; assert_equal([1..3], rs)
  rs = []; NatSet.new(1...3).each_range {|r| rs << r}; assert_equal([1..2], rs)
  rs = []; NatSet.new(1..-1).each_range {|r| rs << r}; assert_equal([1..-1], rs)
end

#test_emptyObject



296
297
298
# File 'lib/natset.rb', line 296

def test_empty
  assert(NatSet.empty.empty?)
end

#test_intersectObject



329
330
331
332
333
334
335
# File 'lib/natset.rb', line 329

def test_intersect
  assert_equal(NatSet.empty, NatSet.empty & NatSet.empty)
  assert_equal(NatSet.empty, NatSet.empty & NatSet.universal)
  assert_equal(NatSet.empty, NatSet.universal & NatSet.empty)
  assert_equal(NatSet.universal, NatSet.universal & NatSet.universal)
  assert_equal(NatSet.new(0), NatSet.new(0, 2) & NatSet.new(0, 1))
end

#test_maxObject



396
397
398
399
400
# File 'lib/natset.rb', line 396

def test_max
  assert_equal(nil, NatSet.new().max)
  assert_equal(10, NatSet.new(1..10).max)
  assert_equal(nil, NatSet.new(1..-1).max)
end

#test_minObject



391
392
393
394
# File 'lib/natset.rb', line 391

def test_min
  assert_equal(nil, NatSet.new().min)
  assert_equal(1, NatSet.new(1..10).min)
end

#test_newObject



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/natset.rb', line 345

def test_new
  assert_equal([1, 2], NatSet.new(1).es)
  assert_equal([1, 3], NatSet.new(1, 2).es)
  assert_equal([1, 4], NatSet.new(1, 2, 3).es)
  assert_equal([1, 4], NatSet.new(1, 3, 2).es)
  assert_equal([10, 21], NatSet.new(10..20).es)
  assert_equal([10, 20], NatSet.new(10...20).es)
  assert_equal([1, 2, 3, 4, 5, 6], NatSet.new(1, 3, 5).es)
  assert_equal([1, 16], NatSet.new(5..15, 1..10).es)
  assert_equal([1, 16], NatSet.new(11..15, 1..10).es)
  assert_raises(ArgumentError) {NatSet.new("a")}
  assert_raises(ArgumentError) {NatSet.new("a".."b")}
  assert_raises(ArgumentError) {NatSet.new(-1)}
  assert_raises(ArgumentError) {NatSet.new(-1..3)}
end

#test_openObject



304
305
306
307
# File 'lib/natset.rb', line 304

def test_open
  assert(!NatSet.empty.open?)
  assert(NatSet.universal.open?)
end

#test_singletonObject



309
310
311
312
# File 'lib/natset.rb', line 309

def test_singleton
  assert_equal(1, NatSet._new(1, 2).singleton?)
  assert_equal(nil, NatSet._new(1, 3).singleton?)
end

#test_splitObject



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/natset.rb', line 361

def test_split
  u = NatSet.universal
  assert_equal([[NatSet.universal]], u.split())
  assert_equal([[NatSet.universal]], u.split(NatSet.empty))
  assert_equal([[NatSet.universal, u]], u.split(u))

  n = NatSet.new(10..20)
  assert_equal([[NatSet.new(0..9, 21..-1)],
                [NatSet.new(10..20), n]],
               u.split(n))

  ns = [NatSet.new(10..20), NatSet.new(10..20)]
  assert_equal([[NatSet.new(0..9, 21..-1)],
                [NatSet.new(10..20), *ns]],
               u.split(*ns))

  ns = [NatSet.new(1..30), NatSet.new(5..40)]
  assert_equal([[NatSet.new(0, 41..-1)],
                [NatSet.new(1..4), ns[0]],
                [NatSet.new(31..40), ns[1]],
                [NatSet.new(5..30), *ns]],
               u.split(*ns))

  ns = [NatSet.new(1..30), NatSet.new(5..20)]
  assert_equal([[NatSet.new(0, 31..-1)],
                [NatSet.new(1..4, 21..30), ns[0]],
                [NatSet.new(5..20), *ns]],
               u.split(*ns))
end

#test_subtractObject



337
338
339
340
341
342
343
# File 'lib/natset.rb', line 337

def test_subtract
  assert_equal(NatSet.empty, NatSet.empty - NatSet.empty)
  assert_equal(NatSet.empty, NatSet.empty - NatSet.universal)
  assert_equal(NatSet.universal, NatSet.universal - NatSet.empty)
  assert_equal(NatSet.empty, NatSet.universal - NatSet.universal)
  assert_equal(NatSet.new(2), NatSet.new(0, 2) - NatSet.new(0, 1))
end

#test_unionObject



321
322
323
324
325
326
327
# File 'lib/natset.rb', line 321

def test_union
  assert_equal(NatSet.empty, NatSet.empty + NatSet.empty)
  assert_equal(NatSet.universal, NatSet.empty + NatSet.universal)
  assert_equal(NatSet.universal, NatSet.universal + NatSet.empty)
  assert_equal(NatSet.universal, NatSet.universal + NatSet.universal)
  assert_equal(NatSet.new(0..2), NatSet.new(0, 2) + NatSet.new(0, 1))
end

#test_universalObject



300
301
302
# File 'lib/natset.rb', line 300

def test_universal
  assert(NatSet.universal.universal?)
end