Esempio n. 1
0
func TestMapEquals(t *testing.T) {
	assert := assert.New(t)

	m1 := NewMap()
	m2 := m1
	m3 := NewMap()

	assert.True(m1.Equals(m2))
	assert.True(m2.Equals(m1))
	assert.True(m3.Equals(m2))
	assert.True(m2.Equals(m3))
	diffMapTest(assert, m1, m2, 0, 0, 0)
	diffMapTest(assert, m1, m3, 0, 0, 0)
	diffMapTest(assert, m2, m1, 0, 0, 0)
	diffMapTest(assert, m2, m3, 0, 0, 0)
	diffMapTest(assert, m3, m1, 0, 0, 0)
	diffMapTest(assert, m3, m2, 0, 0, 0)

	m1 = NewMap(String("foo"), Number(0.0), String("bar"), NewList())
	m2 = m2.SetM(String("foo"), Number(0.0), String("bar"), NewList())
	assert.True(m1.Equals(m2))
	assert.True(m2.Equals(m1))
	assert.False(m2.Equals(m3))
	assert.False(m3.Equals(m2))
	diffMapTest(assert, m1, m2, 0, 0, 0)
	diffMapTest(assert, m1, m3, 2, 0, 0)
	diffMapTest(assert, m2, m1, 0, 0, 0)
	diffMapTest(assert, m2, m3, 2, 0, 0)
	diffMapTest(assert, m3, m1, 0, 2, 0)
	diffMapTest(assert, m3, m2, 0, 2, 0)
}
Esempio n. 2
0
func TestTypeCacheCyclicUnions(t *testing.T) {
	assert := assert.New(t)

	ut := MakeUnionType(MakeCycleType(0), NumberType, StringType, BoolType, BlobType, ValueType, TypeType)
	st := MakeStructType("Foo",
		[]string{"foo"},
		[]*Type{ut},
	)

	assert.True(ut.Desc.(CompoundDesc).ElemTypes[0].Kind() == CycleKind)
	// That the Struct / Cycle landed in index 1 was found empirically.
	assert.True(st == st.Desc.(StructDesc).fields[0].t.Desc.(CompoundDesc).ElemTypes[1])
	// ut contains an explicit Cycle type; noms must not surrepticiously change existing types so we can be sure that the Union within st is different in that the cycle has been resolved.
	assert.False(ut == st.Desc.(StructDesc).fields[0].t)

	// Note that the union in this second case is created with a different ordering of its type arguments.
	ut2 := MakeUnionType(NumberType, StringType, BoolType, BlobType, ValueType, TypeType, MakeCycleType(0))
	st2 := MakeStructType("Foo",
		[]string{"foo"},
		[]*Type{ut2},
	)
	assert.True(ut2.Desc.(CompoundDesc).ElemTypes[0].Kind() == CycleKind)
	assert.True(st2 == st2.Desc.(StructDesc).fields[0].t.Desc.(CompoundDesc).ElemTypes[1])
	assert.False(ut2 == st2.Desc.(StructDesc).fields[0].t)

	assert.True(ut == ut2)
	assert.True(st == st2)
}
Esempio n. 3
0
func TestMapEmpty(t *testing.T) {
	assert := assert.New(t)

	m := NewMap()
	assert.True(m.Empty())
	m = m.Set(Bool(false), String("hi"))
	assert.False(m.Empty())
	m = m.Set(NewList(), NewMap())
	assert.False(m.Empty())
}
Esempio n. 4
0
func TestCauseInTypes(t *testing.T) {
	assert := assert.New(t)

	te := testError{"te"}
	te2 := testError2{"te2"}

	assert.True(causeInTypes(te, te))
	assert.True(causeInTypes(te, te2, te))
	assert.False(causeInTypes(te, te2))
	assert.False(causeInTypes(te))
}
Esempio n. 5
0
func TestMapHasRemove(t *testing.T) {
	assert := assert.New(t)
	m1 := NewMap()
	assert.False(m1.Has(String("foo")))
	m2 := m1.Set(String("foo"), String("foo"))
	assert.False(m1.Has(String("foo")))
	assert.True(m2.Has(String("foo")))
	m3 := m1.Remove(String("foo"))
	assert.False(m1.Has(String("foo")))
	assert.True(m2.Has(String("foo")))
	assert.False(m3.Has(String("foo")))
}
Esempio n. 6
0
func TestSizeCache(t *testing.T) {
	assert := assert.New(t)
	defSize := uint64(200)

	c := New(1024)
	for i, v := range []string{"data-1", "data-2", "data-3", "data-4", "data-5", "data-6", "data-7", "data-8", "data-9"} {
		c.Add(hashFromString(v), defSize, v)
		maxElements := uint64(i + 1)
		if maxElements >= uint64(5) {
			maxElements = uint64(5)
		}
		assert.Equal(maxElements*defSize, c.totalSize)
	}

	_, ok := c.Get(hashFromString("data-1"))
	assert.False(ok)
	assert.Equal(hashFromString("data-5"), c.lru.Front().Value)

	v, ok := c.Get(hashFromString("data-5"))
	assert.True(ok)
	assert.Equal("data-5", v.(string))
	assert.Equal(hashFromString("data-5"), c.lru.Back().Value)
	assert.Equal(hashFromString("data-6"), c.lru.Front().Value)

	c.Add(hashFromString("data-7"), defSize, "data-7")
	assert.Equal(hashFromString("data-7"), c.lru.Back().Value)
	assert.Equal(uint64(1000), c.totalSize)

	c.Add(hashFromString("no-data"), 0, nil)
	v, ok = c.Get(hashFromString("no-data"))
	assert.True(ok)
	assert.Nil(v)
	assert.Equal(hashFromString("no-data"), c.lru.Back().Value)
	assert.Equal(uint64(1000), c.totalSize)
	assert.Equal(6, c.lru.Len())
	assert.Equal(6, len(c.cache))

	for _, v := range []string{"data-5", "data-6", "data-7", "data-8", "data-9"} {
		c.Get(hashFromString(v))
		assert.Equal(hashFromString(v), c.lru.Back().Value)
	}
	assert.Equal(hashFromString("no-data"), c.lru.Front().Value)

	c.Add(hashFromString("data-10"), 200, "data-10")
	assert.Equal(uint64(1000), c.totalSize)
	assert.Equal(5, c.lru.Len())
	assert.Equal(5, len(c.cache))

	_, ok = c.Get(hashFromString("no-data"))
	assert.False(ok)
	_, ok = c.Get(hashFromString("data-5"))
	assert.False(ok)
}
Esempio n. 7
0
func TestStringEquals(t *testing.T) {
	assert := assert.New(t)
	s1 := String("foo")
	s2 := String("foo")
	s3 := s2
	s4 := String("bar")
	assert.True(s1.Equals(s2))
	assert.True(s2.Equals(s1))
	assert.True(s1.Equals(s3))
	assert.True(s3.Equals(s1))
	assert.False(s1.Equals(s4))
	assert.False(s4.Equals(s1))
}
Esempio n. 8
0
func TestMapMaybeGet(t *testing.T) {
	if testing.Short() {
		t.Skip("Skipping test in short mode.")
	}

	smallTestChunks()
	defer normalProductionChunks()

	assert := assert.New(t)

	doTest := func(tm testMap) {
		m := tm.toMap()
		for _, entry := range tm.entries {
			v, ok := m.MaybeGet(entry.key)
			if assert.True(ok, "%v should have been in the map!", entry.key) {
				assert.True(v.Equals(entry.value), "%v != %v", v, entry.value)
			}
		}
		_, ok := m.MaybeGet(tm.knownBadKey)
		assert.False(ok, "m should not contain %v", tm.knownBadKey)
	}

	doTest(getTestNativeOrderMap(2))
	doTest(getTestRefValueOrderMap(2))
	doTest(getTestRefToNativeOrderMap(2, NewTestValueStore()))
	doTest(getTestRefToValueOrderMap(2, NewTestValueStore()))
}
Esempio n. 9
0
func TestMapSetExistingKeyToNewValue(t *testing.T) {
	if testing.Short() {
		t.Skip("Skipping test in short mode.")
	}

	smallTestChunks()
	defer normalProductionChunks()

	assert := assert.New(t)

	tm := getTestNativeOrderMap(2)
	original := tm.toMap()

	expectedWorking := tm
	actual := original
	for i, entry := range tm.entries {
		newValue := Number(int64(entry.value.(Number)) + 1)
		expectedWorking = expectedWorking.SetValue(i, newValue)
		actual = actual.Set(entry.key, newValue)
	}

	expected := expectedWorking.toMap()
	assert.Equal(expected.Len(), actual.Len())
	assert.True(expected.Equals(actual))
	assert.False(original.Equals(actual))
	diffMapTest(assert, expected, actual, 0, 0, 0)
}
Esempio n. 10
0
// AssertNotCalled asserts that the method was not called.
// It can produce a false result when an argument is a pointer type and the underlying value changed after calling the mocked method.
func (m *Mock) AssertNotCalled(t TestingT, methodName string, arguments ...interface{}) bool {
	if !assert.False(t, m.methodWasCalled(methodName, arguments), fmt.Sprintf("The \"%s\" method was called with %d argument(s), but should NOT have been.", methodName, len(arguments))) {
		t.Logf("%v", m.expectedCalls())
		return false
	}
	return true
}
Esempio n. 11
0
func TestGreater(t *testing.T) {
	assert := assert.New(t)

	r1 := Parse("sha1-0000000000000000000000000000000000000001")
	r2 := Parse("sha1-0000000000000000000000000000000000000002")

	assert.False(r1.Greater(r1))
	assert.False(r1.Greater(r2))
	assert.True(r2.Greater(r1))
	assert.False(r2.Greater(r2))

	r0 := Hash{}
	assert.False(r0.Greater(r0))
	assert.False(r0.Greater(r2))
	assert.True(r2.Greater(r0))
}
Esempio n. 12
0
func TestTwoClientsWithEmptyDataset(t *testing.T) {
	assert := assert.New(t)
	id1 := "testdataset"
	cs := chunks.NewMemoryStore()

	dsx := newDS(id1, cs)
	dsy := newDS(id1, cs)

	// dsx: || -> |a|
	a := types.String("a")
	dsx, err := dsx.CommitValue(a)
	assert.NoError(err)
	assert.True(dsx.Head().Get(datas.ValueField).Equals(a))

	// dsy: || -> |b|
	_, ok := dsy.MaybeHead()
	assert.False(ok)
	b := types.String("b")
	dsy, err = dsy.CommitValue(b)
	assert.Error(err)
	// Commit failed, but ds1 now has latest head, so we should be able to just try again.
	// dsy: |a| -> |b|
	dsy, err = dsy.CommitValue(b)
	assert.NoError(err)
	assert.True(dsy.Head().Get(datas.ValueField).Equals(b))
}
Esempio n. 13
0
func TestHeadValueFunctions(t *testing.T) {
	assert := assert.New(t)

	id1 := "testdataset"
	id2 := "otherdataset"
	cs := chunks.NewMemoryStore()

	ds1 := newDS(id1, cs)

	// ds1: |a|
	a := types.String("a")
	ds1, err := ds1.CommitValue(a)
	assert.NoError(err)

	hv := ds1.Head().Get(datas.ValueField)
	assert.Equal(a, hv)
	assert.Equal(a, ds1.HeadValue())

	hv, ok := ds1.MaybeHeadValue()
	assert.True(ok)
	assert.Equal(a, hv)

	ds2 := newDS(id2, cs)
	assert.Panics(func() {
		ds2.HeadValue()
	})
	_, ok = ds2.MaybeHeadValue()
	assert.False(ok)
}
Esempio n. 14
0
func TestLess(t *testing.T) {
	assert := assert.New(t)

	r1 := Parse("00000000000000000000000000000001")
	r2 := Parse("00000000000000000000000000000002")

	assert.False(r1.Less(r1))
	assert.True(r1.Less(r2))
	assert.False(r2.Less(r1))
	assert.False(r2.Less(r2))

	r0 := Hash{}
	assert.False(r0.Less(r0))
	assert.True(r0.Less(r2))
	assert.False(r2.Less(r0))
}
Esempio n. 15
0
func TestTotalOrdering(t *testing.T) {
	assert := assert.New(t)

	// values in increasing order. Some of these are compared by ref so changing the serialization might change the ordering.
	values := []Value{
		Bool(false), Bool(true),
		Number(-10), Number(0), Number(10),
		String("a"), String("b"), String("c"),

		// The order of these are done by the hash.
		NewSet(Number(0), Number(1), Number(2), Number(3)),
		BoolType,

		// Value - values cannot be value
		// Cycle - values cannot be cycle
		// Union - values cannot be unions
	}

	for i, vi := range values {
		for j, vj := range values {
			if i == j {
				assert.True(vi.Equals(vj))
			} else if i < j {
				x := vi.Less(vj)
				assert.True(x)
			} else {
				x := vi.Less(vj)
				assert.False(x)
			}
		}
	}
}
Esempio n. 16
0
func TestCursorGetMaxNPrevItemsWithSingleItemSequence(t *testing.T) {
	assert := assert.New(t)
	cur := newTestSequenceCursor([]interface{}{[]interface{}{100}, []interface{}{101}, []interface{}{102}})

	assert.Equal([]sequenceItem{}, cur.maxNPrevItems(0))
	assert.Equal([]sequenceItem{}, cur.maxNPrevItems(1))
	assert.Equal([]sequenceItem{}, cur.maxNPrevItems(2))
	assert.Equal([]sequenceItem{}, cur.maxNPrevItems(3))
	assert.Equal(0, cur.idx)

	assert.True(cur.advance())
	assert.Equal([]sequenceItem{}, cur.maxNPrevItems(0))
	assert.Equal([]sequenceItem{100}, cur.maxNPrevItems(1))
	assert.Equal([]sequenceItem{100}, cur.maxNPrevItems(2))
	assert.Equal([]sequenceItem{100}, cur.maxNPrevItems(3))
	assert.Equal(0, cur.idx)

	assert.True(cur.advance())
	assert.Equal([]sequenceItem{}, cur.maxNPrevItems(0))
	assert.Equal([]sequenceItem{101}, cur.maxNPrevItems(1))
	assert.Equal([]sequenceItem{100, 101}, cur.maxNPrevItems(2))
	assert.Equal([]sequenceItem{100, 101}, cur.maxNPrevItems(3))
	assert.Equal(0, cur.idx)

	assert.False(cur.advance())
	assert.Equal([]sequenceItem{102}, cur.maxNPrevItems(1))
	assert.Equal([]sequenceItem{101, 102}, cur.maxNPrevItems(2))
	assert.Equal([]sequenceItem{100, 101, 102}, cur.maxNPrevItems(3))
	assert.Equal([]sequenceItem{100, 101, 102}, cur.maxNPrevItems(4))
	assert.Equal(1, cur.idx)
}
Esempio n. 17
0
func TestListSet(t *testing.T) {
	if testing.Short() {
		t.Skip("Skipping test in short mode.")
	}
	assert := assert.New(t)

	smallTestChunks()
	defer normalProductionChunks()

	testList := getTestList()
	cl := testList.toList()

	testIdx := func(idx int, testEquality bool) {
		newVal := Number(-1) // Test values are never < 0
		cl2 := cl.Set(uint64(idx), newVal)
		assert.False(cl.Equals(cl2))
		if testEquality {
			assert.True(testList.Set(idx, newVal).toList().Equals(cl2))
		}
	}

	// Compare list equality. Increment by 100 because each iteration requires building a new list, which is slow, but always test the last index.
	for incr, i := 100, 0; i < len(testList); i += incr {
		testIdx(i, true)
	}
	testIdx(len(testList)-1, true)

	// Compare list unequality, which doesn't require building a new list every iteration, so the increment can be smaller.
	for incr, i := 10, 0; i < len(testList); i += incr {
		testIdx(i, false)
	}
}
Esempio n. 18
0
func TestSetEmptyInsert(t *testing.T) {
	assert := assert.New(t)
	s := NewSet()
	assert.True(s.Empty())
	s = s.Insert(Bool(false))
	assert.False(s.Empty())
	assert.Equal(uint64(1), s.Len())
}
Esempio n. 19
0
func TestZeroSizeCache(t *testing.T) {
	assert := assert.New(t)

	c := New(0)
	c.Add(hashFromString("data1"), 200, "data1")
	_, ok := c.Get(hashFromString("data1"))
	assert.False(ok)
}
Esempio n. 20
0
func TestTooLargeValue(t *testing.T) {
	assert := assert.New(t)

	c := New(1024)
	c.Add(hashFromString("big-data"), 2048, "big-data")
	_, ok := c.Get(hashFromString("big-data"))
	assert.False(ok)
}
Esempio n. 21
0
func Test_Arguments_Is(t *testing.T) {

	var args = Arguments([]interface{}{"string", 123, true})

	assert.True(t, args.Is("string", 123, true))
	assert.False(t, args.Is("wrong", 456, false))

}
Esempio n. 22
0
func TestSetHas(t *testing.T) {
	assert := assert.New(t)
	s1 := NewSet(Bool(true), Number(1), String("hi"))
	assert.True(s1.Has(Bool(true)))
	assert.False(s1.Has(Bool(false)))
	assert.True(s1.Has(Number(1)))
	assert.False(s1.Has(Number(0)))
	assert.True(s1.Has(String("hi")))
	assert.False(s1.Has(String("ho")))

	s2 := s1.Insert(Bool(false))
	assert.True(s2.Has(Bool(false)))
	assert.True(s2.Has(Bool(true)))

	assert.True(s1.Has(Bool(true)))
	assert.False(s1.Has(Bool(false)))
}
Esempio n. 23
0
func TestSetUniqueKeysString(t *testing.T) {
	assert := assert.New(t)
	s1 := NewSet(String("hello"), String("world"), String("hello"))
	assert.Equal(uint64(2), s1.Len())
	assert.True(s1.Has(String("hello")))
	assert.True(s1.Has(String("world")))
	assert.False(s1.Has(String("foo")))
}
func TestCachingChunkHaver(t *testing.T) {
	assert := assert.New(t)
	ts := chunks.NewTestStore()
	ccs := newCachingChunkHaver(ts)
	input := "abc"

	c := chunks.NewChunk([]byte(input))
	assert.False(ccs.Has(c.Hash()))
	assert.Equal(ts.Hases, 1)
	assert.False(ccs.Has(c.Hash()))
	assert.Equal(ts.Hases, 1)

	ts.Put(c)
	ccs = newCachingChunkHaver(ts)
	assert.True(ccs.Has(c.Hash()))
	assert.Equal(ts.Hases, 2)
	assert.True(ccs.Has(c.Hash()))
	assert.Equal(ts.Hases, 2)
}
Esempio n. 25
0
func TestIsEmpty(t *testing.T) {
	r1 := Hash{}
	assert.True(t, r1.IsEmpty())

	r2 := Parse("00000000000000000000000000000000")
	assert.True(t, r2.IsEmpty())

	r3 := Parse("rmnjb8cjc5tblj21ed4qs821649eduie")
	assert.False(t, r3.IsEmpty())
}
Esempio n. 26
0
func TestIsEmpty(t *testing.T) {
	r1 := Hash{}
	assert.True(t, r1.IsEmpty())

	r2 := Parse("sha1-0000000000000000000000000000000000000000")
	assert.True(t, r2.IsEmpty())

	r3 := Parse("sha1-a9993e364706816aba3e25717850c26c9cd0d89d")
	assert.False(t, r3.IsEmpty())
}
Esempio n. 27
0
func TestSetUniqueKeysNumber(t *testing.T) {
	assert := assert.New(t)
	s1 := NewSet(Number(4), Number(1), Number(0), Number(0), Number(1), Number(3))
	assert.Equal(uint64(4), s1.Len())
	assert.True(s1.Has(Number(4)))
	assert.True(s1.Has(Number(1)))
	assert.True(s1.Has(Number(0)))
	assert.True(s1.Has(Number(3)))
	assert.False(s1.Has(Number(2)))
}
Esempio n. 28
0
func Test_Mock_AssertExpectations_With_Repeatability(t *testing.T) {

	var mockedService = new(TestExampleImplementation)

	mockedService.On("Test_Mock_AssertExpectations_With_Repeatability", 1, 2, 3).Return(5, 6, 7).Twice()

	tt := new(testing.T)
	assert.False(t, mockedService.AssertExpectations(tt))

	// make the call now
	mockedService.Called(1, 2, 3)

	assert.False(t, mockedService.AssertExpectations(tt))

	mockedService.Called(1, 2, 3)

	// now assert expectations
	assert.True(t, mockedService.AssertExpectations(tt))

}
Esempio n. 29
0
func TestCompoundMapWithValuesOfEveryType(t *testing.T) {
	assert := assert.New(t)

	v := Number(42)
	kvs := []Value{
		// Values
		Bool(true), v,
		Number(0), v,
		String("hello"), v,
		NewBlob(bytes.NewBufferString("buf")), v,
		NewSet(Bool(true)), v,
		NewList(Bool(true)), v,
		NewMap(Bool(true), Number(0)), v,
		NewStruct("", StructData{"field": Bool(true)}), v,
		// Refs of values
		NewRef(Bool(true)), v,
		NewRef(Number(0)), v,
		NewRef(String("hello")), v,
		NewRef(NewBlob(bytes.NewBufferString("buf"))), v,
		NewRef(NewSet(Bool(true))), v,
		NewRef(NewList(Bool(true))), v,
		NewRef(NewMap(Bool(true), Number(0))), v,
		NewRef(NewStruct("", StructData{"field": Bool(true)})), v,
	}

	m := NewMap(kvs...)
	for i := 1; !isMetaSequence(m.sequence()); i++ {
		k := Number(i)
		kvs = append(kvs, k, v)
		m = m.Set(k, v)
	}

	assert.Equal(len(kvs)/2, int(m.Len()))
	fk, fv := m.First()
	assert.True(bool(fk.(Bool)))
	assert.True(v.Equals(fv))

	for i, kOrV := range kvs {
		if i%2 == 0 {
			assert.True(m.Has(kOrV))
			assert.True(v.Equals(m.Get(kOrV)))
		} else {
			assert.True(v.Equals(kOrV))
		}
	}

	for len(kvs) > 0 {
		k := kvs[0]
		kvs = kvs[2:]
		m = m.Remove(k)
		assert.False(m.Has(k))
		assert.Equal(len(kvs)/2, int(m.Len()))
	}
}
Esempio n. 30
0
func TestSetRemove(t *testing.T) {
	assert := assert.New(t)
	v1 := Bool(false)
	v2 := Bool(true)
	v3 := Number(0)
	s := NewSet(v1, v2, v3)
	assert.True(s.Has(v1))
	assert.True(s.Has(v2))
	assert.True(s.Has(v3))
	s = s.Remove(v1)
	assert.False(s.Has(v1))
	assert.True(s.Has(v2))
	assert.True(s.Has(v3))
	s2 := s.Remove(v2)
	assert.False(s.Has(v1))
	assert.True(s.Has(v2))
	assert.True(s.Has(v3))
	assert.False(s2.Has(v1))
	assert.False(s2.Has(v2))
	assert.True(s2.Has(v3))
}