Exemplo n.º 1
0
func encode(s string) []byte {
	switch *enc {
	case stringFormat:
		return []byte(s)
	case floatFormat:
		result, err := strconv.ParseFloat(s, 64)
		if err != nil {
			panic(err)
		}
		return common.EncodeFloat64(result)
	case intFormat:
		result, err := strconv.ParseInt(s, 10, 64)
		if err != nil {
			panic(err)
		}
		return common.EncodeInt64(result)
	case bigFormat:
		result, ok := new(big.Int).SetString(s, 10)
		if !ok {
			panic(fmt.Errorf("Bad BigInt format: %v", s))
		}
		return common.EncodeBigInt(result)
	}
	panic(fmt.Errorf("Unknown encoding: %v", *enc))
}
Exemplo n.º 2
0
func TestBigIntAdd(t *testing.T) {
	found := bigIntAdd([][]byte{common.EncodeBigInt(big.NewInt(1))}, [][]byte{common.EncodeBigInt(big.NewInt(2)), common.EncodeBigInt(big.NewInt(3)), common.EncodeBigInt(big.NewInt(4))}, 1)
	expected := [][]byte{common.EncodeBigInt(big.NewInt(10))}
	if !reflect.DeepEqual(found, expected) {
		t.Errorf("%v should be %v", fmt.Sprint(common.DecodeBigInt(found[0])), fmt.Sprint(common.DecodeBigInt(expected[0])))
	}
	found = bigIntAdd(nil, [][]byte{common.EncodeBigInt(big.NewInt(1)), common.EncodeBigInt(big.NewInt(2)), common.EncodeBigInt(big.NewInt(3)), common.EncodeBigInt(big.NewInt(4))}, 1)
	expected = [][]byte{common.EncodeBigInt(big.NewInt(10))}
	if !reflect.DeepEqual(found, expected) {
		t.Errorf("%v should be %v", fmt.Sprint(common.DecodeBigInt(found[0])), fmt.Sprint(common.DecodeBigInt(expected[0])))
	}
	found = bigIntAdd([][]byte{common.EncodeBigInt(big.NewInt(1))}, [][]byte{common.EncodeBigInt(big.NewInt(2))}, 2)
	expected = [][]byte{common.EncodeBigInt(big.NewInt(5))}
	if !reflect.DeepEqual(found, expected) {
		t.Errorf("%v should be %v", fmt.Sprint(common.DecodeBigInt(found[0])), fmt.Sprint(common.DecodeBigInt(expected[0])))
	}
}
Exemplo n.º 3
0
func testSetExpression(t *testing.T, c testClient) {
	t1 := []byte("sete1")
	t2 := []byte("sete2")
	for i := byte(0); i < 10; i++ {
		c.SubPut(t1, []byte{i}, common.EncodeBigInt(big.NewInt(1)))
	}
	for i := byte(5); i < 15; i++ {
		c.SubPut(t2, []byte{i}, common.EncodeBigInt(big.NewInt(1)))
	}
	assertSetOps(t, c.SetExpression(setop.SetExpression{
		Op: setop.MustParse("(U:BigIntAnd sete1 sete2)"),
	}), []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, []byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})
	assertSetOps(t, c.SetExpression(setop.SetExpression{
		Min: []byte{2},
		Max: []byte{6},
		Op:  setop.MustParse("(U:BigIntAnd sete1 sete2)"),
	}), []byte{3, 4, 5}, []byte{1, 1, 1})
	assertSetOps(t, c.SetExpression(setop.SetExpression{
		Min:    []byte{2},
		Max:    []byte{6},
		MinInc: true,
		MaxInc: true,
		Op:     setop.MustParse("(U:BigIntAnd sete1 sete2)"),
	}), []byte{2, 3, 4, 5, 6}, []byte{1, 1, 1, 1, 1})
	assertSetOps(t, c.SetExpression(setop.SetExpression{
		Min:    []byte{2},
		Max:    []byte{6},
		MinInc: true,
		MaxInc: true,
		Len:    3,
		Op:     setop.MustParse("(U:BigIntAnd sete1 sete2)"),
	}), []byte{2, 3, 4}, []byte{1, 1, 1})
	assertSetOps(t, c.SetExpression(setop.SetExpression{
		Min:    []byte{2},
		Max:    []byte{6},
		MinInc: true,
		MaxInc: true,
		Len:    3,
		Dest:   []byte("sete3"),
		Op:     setop.MustParse("(U:BigIntAnd sete1 sete2)"),
	}), []byte{}, []byte{})
	min := 0
	max := 100
	assertItems(t, c.SliceIndex([]byte("sete3"), &min, &max), []byte{2, 3, 4}, []byte{1, 1, 1})

	assertSetOps(t, c.SetExpression(setop.SetExpression{
		Code: "(U:BigIntAnd sete1 sete2)",
	}), []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, []byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})
	assertSetOps(t, c.SetExpression(setop.SetExpression{
		Min:  []byte{2},
		Max:  []byte{6},
		Code: "(U:BigIntAnd sete1 sete2)",
	}), []byte{3, 4, 5}, []byte{1, 1, 1})
	assertSetOps(t, c.SetExpression(setop.SetExpression{
		Min:    []byte{2},
		Max:    []byte{6},
		MinInc: true,
		MaxInc: true,
		Code:   "(U:BigIntAnd sete1 sete2)",
	}), []byte{2, 3, 4, 5, 6}, []byte{1, 1, 1, 1, 1})
	assertSetOps(t, c.SetExpression(setop.SetExpression{
		Min:    []byte{2},
		Max:    []byte{6},
		MinInc: true,
		MaxInc: true,
		Len:    3,
		Code:   "(U:BigIntAnd sete1 sete2)",
	}), []byte{2, 3, 4}, []byte{1, 1, 1})
	assertSetOps(t, c.SetExpression(setop.SetExpression{
		Min:    []byte{2},
		Max:    []byte{6},
		MinInc: true,
		MaxInc: true,
		Len:    3,
		Dest:   []byte("sete4"),
		Code:   "(U:BigIntAnd sete1 sete2)",
	}), []byte{}, []byte{})
	assertItems(t, c.SliceIndex([]byte("sete4"), &min, &max), []byte{2, 3, 4}, []byte{1, 1, 1})
}