예제 #1
0
파일: slice_test.go 프로젝트: etcenter/c4
func TestIDofIDSlice(t *testing.T) {
	is := is.New(t)
	var b, s []byte
	for i := 0; i < 64; i++ {
		b = append(b, 0xFF)
		s = append(s, 0x00)
	}
	bigBig := big.NewInt(0)
	bigSmall := big.NewInt(0)
	bigBig = bigBig.SetBytes(b)
	bigSmall = bigSmall.SetBytes(s)
	bigID := asset.ID(*bigBig)
	smallID := asset.ID(*bigSmall)

	encoder := asset.NewIDEncoder()
	is.OK(encoder)
	_, err := io.Copy(encoder, strings.NewReader(`c41111111111111111111111111111111111111111111111111111111111111111111111111111111111111111c467RPWkcUr5dga8jgywjSup7CMoA9FNqkNjEFgAkEpF9vNktFnx77e2Js11EDL3BNu9MaKFUbacZRt1HYym4b8RNp`))
	is.NoErr(err)
	id := encoder.ID()

	var idSlice asset.IDSlice
	idSlice.Push(&bigID)
	idSlice.Push(&smallID)
	sliceID, err := idSlice.ID()
	is.NoErr(err)

	is.Equal(sliceID.String(), id.String())
}
예제 #2
0
파일: walker.go 프로젝트: etcenter/c4
func walkFilesystem(depth int, filename string, relative_path string) (*asset.ID, error) {
	path, err := filepath.Abs(filename)
	if err != nil {
		return nil, err
	}

	item := &FsItem{}
	err = item.Stat(path)
	if err != nil {
		return nil, err
	}

	if item.IsFile() {
		id, err := item.Identify()
		if err != nil {
			return nil, err
		}
		item.Id = id
	} else if item.Link {
		newFilepath, err := filepath.EvalSymlinks(filename)
		if err != nil {
			return nil, err
		}
		item.LinkPath = &newFilepath
		if links_flag { // Then follow the link
			id, err := walkFilesystem(depth, newFilepath, relative_path)
			if err != nil {
				return item.Id, err
			}
			item.Id = id
		}
	} else if item.Folder {
		files, err := ioutil.ReadDir(path)
		if err != nil {
			return item.Id, err
		}
		var childIDs asset.IDSlice
		for _, file := range files {
			path := filename + string(filepath.Separator) + file.Name()
			id, err := walkFilesystem(depth-1, path, relative_path)
			if err != nil {
				return item.Id, err
			}
			childIDs.Push(id)
		}
		id, err := childIDs.ID()
		if err != nil {
			return item.Id, err
		}
		item.Id = id
	}
	if depth >= 0 || recursive_flag {
		output(item)
	}
	return item.Id, nil
}
예제 #3
0
파일: slice_test.go 프로젝트: etcenter/c4
func TestIDSliceString(t *testing.T) {
	is := is.New(t)

	var ids asset.IDSlice
	id1, err := asset.Identify(strings.NewReader("foo"))
	is.NoErr(err)
	id2, err := asset.Identify(strings.NewReader("bar"))
	is.NoErr(err)

	ids.Push(id1)
	ids.Push(id2)

	is.Equal(ids.String(), id1.String()+id2.String())
}
예제 #4
0
파일: slice_test.go 프로젝트: etcenter/c4
func TestIDSliceSearchIDs(t *testing.T) {
	is := is.New(t)

	var ids asset.IDSlice
	id1, err := asset.Identify(strings.NewReader("foo"))
	is.NoErr(err)
	id2, err := asset.Identify(strings.NewReader("bar"))
	is.NoErr(err)
	id3, err := asset.Identify(strings.NewReader("baz"))
	is.NoErr(err)

	ids.Push(id1)
	ids.Push(id2)
	ids.Push(id3)
	ids.Sort()

	is.True(id2.Less(id1))
	is.True(id3.Less(id2))

	is.Equal(asset.SearchIDs(ids, id1), 2)
	is.Equal(asset.SearchIDs(ids, id2), 1)
	is.Equal(asset.SearchIDs(ids, id3), 0)
}
예제 #5
0
파일: slice_test.go 프로젝트: etcenter/c4
func TestIDSliceSort(t *testing.T) {
	is := is.New(t)
	var b, s []byte
	for i := 0; i < 64; i++ {
		b = append(b, 0xFF)
		s = append(s, 0x00)
	}
	bigBig := big.NewInt(0)
	bigSmall := big.NewInt(0)
	bigBig = bigBig.SetBytes(b)
	bigSmall = bigSmall.SetBytes(s)
	bigID := asset.ID(*bigBig)
	smallID := asset.ID(*bigSmall)

	var idSlice asset.IDSlice

	idSlice.Push(&bigID)
	idSlice.Push(&smallID)
	is.Equal(idSlice[0].String(), `c467RPWkcUr5dga8jgywjSup7CMoA9FNqkNjEFgAkEpF9vNktFnx77e2Js11EDL3BNu9MaKFUbacZRt1HYym4b8RNp`)
	is.Equal(idSlice[1].String(), `c41111111111111111111111111111111111111111111111111111111111111111111111111111111111111111`)
	idSlice.Sort()
	is.Equal(idSlice[0].String(), `c41111111111111111111111111111111111111111111111111111111111111111111111111111111111111111`)
	is.Equal(idSlice[1].String(), `c467RPWkcUr5dga8jgywjSup7CMoA9FNqkNjEFgAkEpF9vNktFnx77e2Js11EDL3BNu9MaKFUbacZRt1HYym4b8RNp`)
}