gonum.org/v1/gonum@v0.14.0/graph/formats/rdf/urna_test.go (about)

     1  // Copyright ©2020 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package rdf
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"io"
    11  	"os"
    12  	"path/filepath"
    13  	"strings"
    14  	"testing"
    15  
    16  	"github.com/google/go-cmp/cmp"
    17  )
    18  
    19  var deduplicateTests = []struct {
    20  	statements string
    21  	want       string
    22  }{
    23  	{},
    24  	{
    25  		statements: `
    26  _:1 <p:a> _:2 .
    27  `,
    28  		want: `_:1 <p:a> _:2 .
    29  `,
    30  	},
    31  	{
    32  		statements: `
    33  _:1 <p:a> _:2 .
    34  _:1 <p:a> _:2 .
    35  `,
    36  		want: `_:1 <p:a> _:2 .
    37  `,
    38  	},
    39  	{
    40  		statements: `
    41  _:1 <p:a> _:2 .
    42  _:2 <p:a> _:1 .
    43  _:1 <p:a> _:2 .
    44  `,
    45  		want: `_:1 <p:a> _:2 .
    46  _:2 <p:a> _:1 .
    47  `,
    48  	},
    49  	{
    50  		statements: `
    51  _:1 <p:a> _:2 .
    52  _:2 <p:a> _:1 .
    53  _:1 <p:a> _:2 .
    54  _:1 <p:a> _:2 .
    55  `,
    56  		want: `_:1 <p:a> _:2 .
    57  _:2 <p:a> _:1 .
    58  `,
    59  	},
    60  }
    61  
    62  func TestDeduplicate(t *testing.T) {
    63  tests:
    64  	for i, test := range deduplicateTests {
    65  		var statements []*Statement
    66  		dec := NewDecoder(strings.NewReader(test.statements))
    67  		for {
    68  			s, err := dec.Unmarshal()
    69  			if err != nil {
    70  				if err != io.EOF {
    71  					t.Errorf("error during decoding: %v", err)
    72  					continue tests
    73  				}
    74  				break
    75  			}
    76  			statements = append(statements, s)
    77  		}
    78  
    79  		var buf strings.Builder
    80  		for _, s := range Deduplicate(statements) {
    81  			fmt.Fprintln(&buf, s)
    82  		}
    83  
    84  		got := buf.String()
    85  		if got != test.want {
    86  			t.Errorf("unexpected result for test %d:\n%s", i, cmp.Diff(got, test.want))
    87  		}
    88  	}
    89  }
    90  
    91  func TestURNA(t *testing.T) {
    92  	glob, err := filepath.Glob(filepath.Join("testdata", *tests))
    93  	if err != nil {
    94  		t.Fatalf("Failed to open test suite: %v", err)
    95  	}
    96  	for _, test := range []struct {
    97  		name  string
    98  		fn    func(dst, src []*Statement) ([]*Statement, error)
    99  		truth string
   100  	}{
   101  		{
   102  			name:  "URDNA2015",
   103  			fn:    URDNA2015,
   104  			truth: "-urdna2015.nq",
   105  		},
   106  		{
   107  			name:  "URGNA2012",
   108  			fn:    URGNA2012,
   109  			truth: "-urgna2012.nq",
   110  		},
   111  	} {
   112  		t.Run(test.name, func(t *testing.T) {
   113  			for _, path := range glob {
   114  				name := filepath.Base(path)
   115  				golden := strings.TrimSuffix(path, "-in.nq") + test.truth
   116  				want, err := os.ReadFile(golden)
   117  				if err != nil {
   118  					if !os.IsNotExist(err) {
   119  						t.Errorf("Failed to read golden data: %v", err)
   120  					}
   121  					continue
   122  				}
   123  
   124  				t.Run(name, func(t *testing.T) {
   125  					f, err := os.Open(path)
   126  					if err != nil {
   127  						t.Fatalf("Failed to open test suite in %q: %v", path, err)
   128  					}
   129  					var statements []*Statement
   130  					dec := NewDecoder(f)
   131  					for {
   132  						s, err := dec.Unmarshal()
   133  						if err != nil {
   134  							if err == io.EOF {
   135  								break
   136  							}
   137  							t.Fatalf("Unexpected error reading from %q: %v", path, err)
   138  						}
   139  						statements = append(statements, s)
   140  					}
   141  					f.Close()
   142  
   143  					relabeled, _ := test.fn(nil, statements)
   144  
   145  					var buf bytes.Buffer
   146  					for _, s := range relabeled {
   147  						fmt.Fprintln(&buf, s)
   148  					}
   149  					got := buf.Bytes()
   150  
   151  					if !bytes.Equal(got, want) {
   152  						t.Errorf("Unexpected result for %s %s:\ngot:\n%s\nwant:\n%s",
   153  							test.name, path, got, want)
   154  					}
   155  				})
   156  			}
   157  		})
   158  	}
   159  }
   160  
   161  func BenchmarkURNA(b *testing.B) {
   162  	benchmarks := []string{
   163  		"test019-in.nq",
   164  		"test044-in.nq",
   165  	}
   166  
   167  	for _, name := range benchmarks {
   168  		path := filepath.Join("testdata", name)
   169  		b.Run(name, func(b *testing.B) {
   170  			f, err := os.Open(path)
   171  			if err != nil {
   172  				b.Fatalf("Failed to open test suite in %q: %v", path, err)
   173  			}
   174  			var statements []*Statement
   175  			dec := NewDecoder(f)
   176  			for {
   177  				s, err := dec.Unmarshal()
   178  				if err != nil {
   179  					if err == io.EOF {
   180  						break
   181  					}
   182  					b.Fatalf("Unexpected error reading from %q: %v", path, err)
   183  				}
   184  				statements = append(statements, s)
   185  			}
   186  			f.Close()
   187  
   188  			for _, bench := range []struct {
   189  				name string
   190  				fn   func(dst, src []*Statement) ([]*Statement, error)
   191  			}{
   192  				{
   193  					name: "URDNA2015",
   194  					fn:   URDNA2015,
   195  				},
   196  				{
   197  					name: "URGNA2012",
   198  					fn:   URGNA2012,
   199  				},
   200  			} {
   201  				b.Run(bench.name, func(b *testing.B) {
   202  					for i := 0; i < b.N; i++ {
   203  						relabeled, _ := bench.fn(nil, statements)
   204  						if len(relabeled) != len(statements) {
   205  							b.Fatalf("unexpected number of relabeled statements: %d != %d", len(relabeled), len(statements))
   206  						}
   207  					}
   208  				})
   209  			}
   210  		})
   211  	}
   212  }