package graph

import (
	"fmt"
	"go-ethereum-advance/common"
	"testing"
)

func TestGraph(t *testing.T) {

	add1 := common.HexToAddress("0x41b84E81Ba73ab6910ac9b2038d6F18C15a05367")
	add2 := common.HexToAddress("0x0297545eCb2686062fF91D187bf21CDdE63ca28B")
	add3 := common.HexToAddress("0x8351C250bEC54B323c6B38430C473E583834cD5A")
	add4 := common.HexToAddress("0xAa61feAb6EECcE67C8BC9f40D88de35C3E86F221")

	fmt.Printf("add1 %v \n", add1)
	fmt.Printf("add2 %v \n", add2)
	fmt.Printf("add3 %v \n", add3)
	fmt.Printf("add4 %v \n", add4)

	graph := NewSparseGraph()

	graph.AddEdge(add1, add2, true)
	graph.AddEdge(add1, add3, true)
	graph.AddEdge(add1, add4, false)

	graph.AddEdge(add2, add3, false)
	graph.AddEdge(add2, add4, false)

	list1, _ := graph.GetContact(add1)
	list2, _ := graph.GetContact(add2)
	list3, _ := graph.GetContact(add3)
	list4, _ := graph.GetContact(add4)

	fmt.Printf("add1:%s :[%v] \n", add1.String(), list1)
	fmt.Printf("add2:%s :[%v] \n", add2.String(), list2)
	fmt.Printf("add3:%s :[%v] \n", add3.String(), list3)
	fmt.Printf("add4:%s :[%v] \n", add4.String(), list4)

	println("=========================================")

	graph.DelEdge(add1, add3, true)
	list5, _ := graph.GetContact(add1)
	list6, _ := graph.GetContact(add2)
	list7, _ := graph.GetContact(add3)
	list8, _ := graph.GetContact(add4)
	//if ok {
	//	fmt.Printf("add1:%s :[%v] \n", add1.String(), list5)
	//}
	//fmt.Printf("add1:%s :[%v] \n", add1.String(), list1)
	//fmt.Printf("add2:%s :[%v] \n", add2.String(), list2)
	//fmt.Printf("add3:%s :[%v] \n", add3.String(), list3)
	//fmt.Printf("add4:%s :[%v] \n", add4.String(), list4)
	fmt.Printf("add1:%s :[%v] \n", add1.String(), list5)
	fmt.Printf("add2:%s :[%v] \n", add2.String(), list6)
	fmt.Printf("add3:%s :[%v] \n", add3.String(), list7)
	fmt.Printf("add4:%s :[%v] \n", add4.String(), list8)

	data, err := graph.Encode()
	if err != nil {
		fmt.Printf("编码出错 %v", err)
	}

	newGraph := NewSparseGraph()
	err = newGraph.Decode(data)
	if err != nil {
		fmt.Printf("解码成功 %v", err)
	}

	println("============================")

	ll, _ := newGraph.GetContact(add1)

	fmt.Printf("add1:%s :[%v] \n", add1.String(), ll)

	nodeNum := newGraph.N
	fmt.Printf("节点数 %d", nodeNum)
}
