Example #1
0
func TestHashSet(t *testing.T) {
	var h fs.HashSet

	if h.Count() != 0 {
		t.Fatalf("expected empty set to have 0 elements, got %d", h.Count())
	}
	a := h.Array()
	if len(a) != 0 {
		t.Fatalf("expected empty slice, got %d", len(a))
	}

	h = h.Add(fs.HashMD5)
	if h.Count() != 1 {
		t.Fatalf("expected 1 element, got %d", h.Count())
	}
	if h.GetOne() != fs.HashMD5 {
		t.Fatalf("expected HashMD5, got %v", h.GetOne())
	}
	a = h.Array()
	if len(a) != 1 {
		t.Fatalf("expected 1 element, got %d", len(a))
	}
	if a[0] != fs.HashMD5 {
		t.Fatalf("expected HashMD5, got %v", a[0])
	}

	// Test overlap, with all hashes
	h = h.Overlap(fs.SupportedHashes)
	if h.Count() != 1 {
		t.Fatalf("expected 1 element, got %d", h.Count())
	}
	if h.GetOne() != fs.HashMD5 {
		t.Fatalf("expected HashMD5, got %v", h.GetOne())
	}
	if !h.SubsetOf(fs.SupportedHashes) {
		t.Fatalf("expected to be subset of all hashes")
	}
	if !h.SubsetOf(fs.NewHashSet(fs.HashMD5)) {
		t.Fatalf("expected to be subset of itself")
	}

	h = h.Add(fs.HashSHA1)
	if h.Count() != 2 {
		t.Fatalf("expected 2 elements, got %d", h.Count())
	}
	one := h.GetOne()
	if !(one == fs.HashMD5 || one == fs.HashSHA1) {
		t.Fatalf("expected to be either MD5 or SHA1, got %v", one)
	}
	if !h.SubsetOf(fs.SupportedHashes) {
		t.Fatalf("expected to be subset of all hashes")
	}
	if h.SubsetOf(fs.NewHashSet(fs.HashMD5)) {
		t.Fatalf("did not expect to be subset of only MD5")
	}
	if h.SubsetOf(fs.NewHashSet(fs.HashSHA1)) {
		t.Fatalf("did not expect to be subset of only SHA1")
	}
	if !h.SubsetOf(fs.NewHashSet(fs.HashMD5, fs.HashSHA1)) {
		t.Fatalf("expected to be subset of MD5/SHA1")
	}
	a = h.Array()
	if len(a) != 2 {
		t.Fatalf("expected 2 elements, got %d", len(a))
	}

	ol := h.Overlap(fs.NewHashSet(fs.HashMD5))
	if ol.Count() != 1 {
		t.Fatalf("expected 1 element overlap, got %d", ol.Count())
	}
	if !ol.Contains(fs.HashMD5) {
		t.Fatalf("expected overlap to be MD5, got %v", ol)
	}
	if ol.Contains(fs.HashSHA1) {
		t.Fatalf("expected overlap NOT to contain SHA1, got %v", ol)
	}

	ol = h.Overlap(fs.NewHashSet(fs.HashMD5, fs.HashSHA1))
	if ol.Count() != 2 {
		t.Fatalf("expected 2 element overlap, got %d", ol.Count())
	}
	if !ol.Contains(fs.HashMD5) {
		t.Fatalf("expected overlap to contain MD5, got %v", ol)
	}
	if !ol.Contains(fs.HashSHA1) {
		t.Fatalf("expected overlap to contain SHA1, got %v", ol)
	}
}
Example #2
0
func TestHashSet(t *testing.T) {
	var h fs.HashSet

	assert.Equal(t, 0, h.Count())

	a := h.Array()
	assert.Len(t, a, 0)

	h = h.Add(fs.HashMD5)
	assert.Equal(t, 1, h.Count())
	assert.Equal(t, fs.HashMD5, h.GetOne())
	a = h.Array()
	assert.Len(t, a, 1)
	assert.Equal(t, a[0], fs.HashMD5)

	// Test overlap, with all hashes
	h = h.Overlap(fs.SupportedHashes)
	assert.Equal(t, 1, h.Count())
	assert.Equal(t, fs.HashMD5, h.GetOne())
	assert.True(t, h.SubsetOf(fs.SupportedHashes))
	assert.True(t, h.SubsetOf(fs.NewHashSet(fs.HashMD5)))

	h = h.Add(fs.HashSHA1)
	assert.Equal(t, 2, h.Count())
	one := h.GetOne()
	if !(one == fs.HashMD5 || one == fs.HashSHA1) {
		t.Fatalf("expected to be either MD5 or SHA1, got %v", one)
	}
	assert.True(t, h.SubsetOf(fs.SupportedHashes))
	assert.False(t, h.SubsetOf(fs.NewHashSet(fs.HashMD5)))
	assert.False(t, h.SubsetOf(fs.NewHashSet(fs.HashSHA1)))
	assert.True(t, h.SubsetOf(fs.NewHashSet(fs.HashMD5, fs.HashSHA1)))
	a = h.Array()
	assert.Len(t, a, 2)

	ol := h.Overlap(fs.NewHashSet(fs.HashMD5))
	assert.Equal(t, 1, ol.Count())
	assert.True(t, ol.Contains(fs.HashMD5))
	assert.False(t, ol.Contains(fs.HashSHA1))

	ol = h.Overlap(fs.NewHashSet(fs.HashMD5, fs.HashSHA1))
	assert.Equal(t, 2, ol.Count())
	assert.True(t, ol.Contains(fs.HashMD5))
	assert.True(t, ol.Contains(fs.HashSHA1))
}