initial
This commit is contained in:
commit
996abb2681
|
@ -0,0 +1,39 @@
|
|||
package chaos
|
||||
|
||||
import "fmt"
|
||||
|
||||
type TPSet struct {
|
||||
add *GSet
|
||||
remove *GSet
|
||||
}
|
||||
|
||||
func NewTPSet() *TPSet {
|
||||
return &TPSet{add: NewGSet(), remove: NewGSet()}
|
||||
}
|
||||
|
||||
func (tpset TPSet) Lookup(element string) bool {
|
||||
return tpset.add.Lookup(element) && !tpset.remove.Lookup(element)
|
||||
}
|
||||
|
||||
func (tpset *TPSet) Add(element string) {
|
||||
tpset.add.Add(element)
|
||||
}
|
||||
|
||||
func (tpset *TPSet) Remove(element string) {
|
||||
tpset.remove.Add(element)
|
||||
}
|
||||
|
||||
func (tpset TPSet) Compare(set *TPSet) bool {
|
||||
return tpset.add.Compare(set.add) && tpset.remove.Compare(set.remove)
|
||||
}
|
||||
|
||||
func (tpset *TPSet) Merge(set *TPSet) *TPSet {
|
||||
ret := new(TPSet)
|
||||
ret.add = tpset.add.Merge(set.add)
|
||||
ret.remove = tpset.remove.Merge(set.remove)
|
||||
return ret
|
||||
}
|
||||
|
||||
func (tpset TPSet) String() string {
|
||||
return fmt.Sprintf("add: %v, remove: %v", tpset.add, tpset.remove)
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package chaos
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestNewTPSet(t *testing.T) {
|
||||
a := NewTPSet()
|
||||
b := NewTPSet()
|
||||
|
||||
a.Add("1")
|
||||
a.Add("2")
|
||||
a.Add("3")
|
||||
a.Add("4")
|
||||
b.Merge(a)
|
||||
|
||||
a.Remove("2")
|
||||
b.Remove("3")
|
||||
a.Merge(b)
|
||||
b.Merge(a)
|
||||
|
||||
if a.Compare(b) == false {
|
||||
t.Fatalf("a and b should be the same: a:%v b:%v", a, b)
|
||||
}
|
||||
|
||||
if a.Lookup("3") != false || b.Lookup("3") != false {
|
||||
t.Fatalf("3 should not be in a %v or b %v", a.Lookup("3"), b.Lookup("3"))
|
||||
}
|
||||
|
||||
t.Logf("a %v", a)
|
||||
t.Logf("b %v", b)
|
||||
}
|
|
@ -0,0 +1,86 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.openprivacy.ca/sarah/chaos"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
)
|
||||
|
||||
func cmd_new(args []string) {
|
||||
if len(args) == 2 {
|
||||
switch args[0] {
|
||||
case "gset":
|
||||
path := path.Join(".", ".chaos", args[1])
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
fmt.Printf("Chaos structure \"%v\" already exists\n", args[1])
|
||||
return
|
||||
}
|
||||
gset := chaos.NewGSet()
|
||||
json, _ := json.Marshal(gset)
|
||||
ioutil.WriteFile(path, json, 0644)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func cmd_add(args []string) {
|
||||
path := path.Join(".", ".chaos", args[0])
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
data, _ := ioutil.ReadFile(path)
|
||||
gset := chaos.NewGSet()
|
||||
err := json.Unmarshal(data, gset)
|
||||
if err != nil {
|
||||
fmt.Printf("Cannot parse \"%v\" as G-Set", args[0])
|
||||
return
|
||||
}
|
||||
gset.Add(args[1])
|
||||
fmt.Printf("Current %v\n", gset)
|
||||
json, _ := json.Marshal(gset)
|
||||
ioutil.WriteFile(path, json, 0644)
|
||||
} else {
|
||||
fmt.Printf("Chaos structure \"%v\" does not exist\n", args[1])
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func cmd_lookup(args []string) {
|
||||
if len(args) == 2 {
|
||||
path := path.Join(".", ".chaos", args[0])
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
data, _ := ioutil.ReadFile(path)
|
||||
gset := chaos.NewGSet()
|
||||
err := json.Unmarshal(data, gset)
|
||||
if err != nil {
|
||||
fmt.Printf("Cannot parse \"%v\" as G-Set", args[0])
|
||||
return
|
||||
}
|
||||
fmt.Printf("%v", gset.Lookup(args[1]))
|
||||
} else {
|
||||
fmt.Printf("Chaos structure \"%v\" does not exist\n", args[0])
|
||||
return
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("Usage: chaos lookup <structure> <key>\n")
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) >= 2 {
|
||||
switch os.Args[1] {
|
||||
case "init":
|
||||
os.Mkdir("./.chaos", 0755)
|
||||
case "new":
|
||||
cmd_new(os.Args[2:])
|
||||
case "add":
|
||||
cmd_add(os.Args[2:])
|
||||
case "lookup":
|
||||
cmd_lookup(os.Args[2:])
|
||||
default:
|
||||
fmt.Printf("Unknown command %v", os.Args[1])
|
||||
}
|
||||
os.Exit(0)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package chaos
|
||||
|
||||
type GSet struct {
|
||||
Elements map[string]bool `json: "Elements"`
|
||||
}
|
||||
|
||||
func NewGSet() *GSet {
|
||||
return &GSet{Elements: make(map[string]bool)}
|
||||
}
|
||||
|
||||
func (gs *GSet) Add(element string) {
|
||||
gs.Elements[element] = true
|
||||
}
|
||||
|
||||
func (gs *GSet) Lookup(element string) bool {
|
||||
_, exists := gs.Elements[element]
|
||||
return exists
|
||||
}
|
||||
|
||||
func (gs *GSet) Compare(gset *GSet) bool {
|
||||
for k := range gs.Elements {
|
||||
hasK := gset.Lookup(k)
|
||||
if !hasK {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (gs *GSet) Merge(gset *GSet) *GSet {
|
||||
for k := range gset.Elements {
|
||||
gs.Add(k)
|
||||
}
|
||||
return gs
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package chaos
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestNewGSet(t *testing.T) {
|
||||
a := NewGSet()
|
||||
b := NewGSet()
|
||||
|
||||
a.Add("1")
|
||||
a.Add("2")
|
||||
a.Add("3")
|
||||
a.Add("4")
|
||||
|
||||
a.Add("1")
|
||||
a.Add("2")
|
||||
a.Add("3")
|
||||
a.Add("4")
|
||||
|
||||
b.Add("1")
|
||||
b.Add("2")
|
||||
b.Add("3")
|
||||
b.Add("4")
|
||||
|
||||
a.Add("5")
|
||||
|
||||
if b.Compare(a) == false {
|
||||
t.Fatalf("b %v should be a subset of a %v", a, b)
|
||||
}
|
||||
|
||||
t.Logf("a: %v", a)
|
||||
t.Logf("b: %v", b)
|
||||
|
||||
b.Merge(a)
|
||||
a.Merge(b)
|
||||
|
||||
if b.Compare(a) == false {
|
||||
t.Fatalf("b %v should be a subset of a %v", a, b)
|
||||
}
|
||||
|
||||
t.Logf("a: %v", a)
|
||||
t.Logf("b: %v", b)
|
||||
|
||||
}
|
Loading…
Reference in New Issue