-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
264 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package rdf | ||
|
||
import "testing" | ||
|
||
// https://www.w3.org/TR/xmlschema-2/#decimal | ||
func TestDataType_NativeType_decimal(t *testing.T) { | ||
for _, test := range []string{ | ||
"-1", "12678967", "+100000", "210", | ||
"-1.23", "12678967.543233", "+100000.00", "210", | ||
} { | ||
if _, ok, err := XSDDecimal.NativeType(test); !ok || err != nil { | ||
t.Errorf("XSDDecimal.NativeType(%q) = %v, %v; want %v, nil", test, ok, err, true) | ||
} | ||
} | ||
for _, test := range []string{ | ||
"-1E4", "1267.43233E12", "12.78e-2", "INF", | ||
} { | ||
if _, ok, err := XSDDecimal.NativeType(test); ok || err == nil { | ||
t.Errorf("XSDDecimal.NativeType(%q) = %v, %v; want %v, nil", test, ok, err, false) | ||
} | ||
} | ||
} | ||
|
||
// https://www.w3.org/TR/xmlschema-2/#double | ||
func TestDataType_NativeType_double(t *testing.T) { | ||
for _, test := range []string{ | ||
"-1", "12678967", "+100000", "210", | ||
"-1.23", "12678967.543233", "+100000.00", "210", | ||
"-1E4", "1267.43233E12", "12.78e-2", "12", "-0", "0", "INF", | ||
} { | ||
if _, ok, err := XSDDouble.NativeType(test); !ok || err != nil { | ||
t.Errorf("XSDDouble.NativeType(%q) = %v, %v; want %v, nil", test, ok, err, true) | ||
} | ||
} | ||
} | ||
|
||
// https://www.w3.org/TR/xmlschema-2/#integer | ||
func TestDataType_NativeType_integer(t *testing.T) { | ||
for _, test := range []string{ | ||
"-1", "12678967", "+100000", "210", | ||
} { | ||
if _, ok, err := XSDInteger.NativeType(test); !ok || err != nil { | ||
t.Errorf("XSDInteger.NativeType(%q) = %v, %v; want %v, nil", test, ok, err, true) | ||
} | ||
} | ||
for _, test := range []string{ | ||
"-1.23", "12678967.543233", "+100000.00", | ||
"-1E4", "1267.43233E12", "12.78e-2", "INF", | ||
} { | ||
if _, ok, err := XSDInteger.NativeType(test); ok || err == nil { | ||
t.Errorf("XSDInteger.NativeType(%q) = %v, %v; want %v, nil", test, ok, err, false) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package rdf | ||
|
||
type Graph struct { | ||
triples map[*Triple]struct{} | ||
} | ||
|
||
func NewGraph(ts ...*Triple) *Graph { | ||
triples := make(map[*Triple]struct{}) | ||
for _, t := range ts { | ||
triples[t] = struct{}{} | ||
} | ||
return &Graph{triples} | ||
} | ||
|
||
func (g *Graph) Add(s, p, o Node) { | ||
t := &Triple{s, p, o} | ||
g.triples[t] = struct{}{} | ||
} | ||
|
||
// Find returns the first triple matching the given pattern. | ||
func (g *Graph) Find(s, p, o Node) *Triple { | ||
for t := range g.iter() { | ||
if (s == nil || t.Subject == s) && | ||
(p == nil || t.Predicate == p) && | ||
(o == nil || t.Object == o) { | ||
return t | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// FindAll returns all triples matching the given pattern. | ||
func (g *Graph) FindAll(s, p, o Node) []*Triple { | ||
var triples []*Triple | ||
for t := range g.iter() { | ||
if (s == nil || t.Subject == s) && | ||
(p == nil || t.Predicate == p) && | ||
(o == nil || t.Object == o) { | ||
triples = append(triples, t) | ||
} | ||
} | ||
return triples | ||
} | ||
|
||
func (g *Graph) Remove(t *Triple) { | ||
delete(g.triples, t) | ||
} | ||
|
||
func (g *Graph) iter() chan *Triple { | ||
ch := make(chan *Triple) | ||
go func() { | ||
for t := range g.triples { | ||
ch <- t | ||
} | ||
close(ch) | ||
}() | ||
return ch | ||
} | ||
|
||
type Triple struct { | ||
Subject Node | ||
Predicate Node | ||
Object Node | ||
} | ||
|
||
func NewTriple(s, p, o Node) *Triple { | ||
return &Triple{s, p, o} | ||
} | ||
|
||
func (t *Triple) Equal(other *Triple) bool { | ||
return t.Subject.Equal(other.Subject) && t.Predicate.Equal(other.Predicate) && t.Object.Equal(other.Object) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package rdf | ||
|
||
import "testing" | ||
|
||
func TestGraph(t *testing.T) { | ||
var ( | ||
a = &BlankNode{"a"} | ||
b = &BlankNode{"b"} | ||
c = &BlankNode{"c"} | ||
) | ||
|
||
abc := NewTriple(a, b, c) | ||
abb := NewTriple(a, b, b) | ||
aaa := NewTriple(a, a, a) | ||
|
||
g := NewGraph(abc, abb, aaa) | ||
|
||
if triple := g.Find(nil, nil, nil); triple != abc { | ||
t.Error("expected abc") | ||
} | ||
if triple := g.Find(a, b, c); triple != abc { | ||
t.Error("expected abc") | ||
} | ||
if triple := g.Find(a, a, a); t == nil || triple != aaa { | ||
t.Error("expected aaa") | ||
} | ||
if triple := g.Find(nil, a, nil); triple != aaa { | ||
t.Error("expected aaa") | ||
} | ||
|
||
if len(g.FindAll(a, nil, nil)) != 3 { | ||
t.Error("expected 3 triples") | ||
} | ||
if len(g.FindAll(nil, b, nil)) != 2 { | ||
t.Error("expected 2 triples") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters