1 /* 2 Copyright 2016 The TensorFlow Authors. All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package tensorflow 18 19 import ( 20 "bytes" 21 "fmt" 22 "testing" 23 ) 24 25 func hasOperations(g *Graph, ops ...string) error { 26 var missing []string 27 for _, op := range ops { 28 if g.Operation(op) == nil { 29 missing = append(missing, op) 30 } 31 } 32 if len(missing) != 0 { 33 return fmt.Errorf("Graph does not have the operations %v", missing) 34 } 35 36 inList := map[string]bool{} 37 for _, op := range g.Operations() { 38 inList[op.Name()] = true 39 } 40 41 for _, op := range ops { 42 if !inList[op] { 43 missing = append(missing, op) 44 } 45 } 46 47 if len(missing) != 0 { 48 return fmt.Errorf("Operations %v are missing from graph.Operations()", missing) 49 } 50 51 return nil 52 } 53 54 func TestGraphWriteToAndImport(t *testing.T) { 55 // Construct a graph 56 g := NewGraph() 57 v, err := NewTensor(int64(1)) 58 if err != nil { 59 t.Fatal(err) 60 } 61 input, err := Placeholder(g, "input", v.DataType()) 62 if err != nil { 63 t.Fatal(err) 64 } 65 if _, err := Neg(g, "neg", input); err != nil { 66 t.Fatal(err) 67 } 68 69 // Serialize the graph 70 buf := new(bytes.Buffer) 71 if _, err := g.WriteTo(buf); err != nil { 72 t.Fatal(err) 73 } 74 75 // Import it into the same graph, with a prefix 76 if err := g.Import(buf.Bytes(), "imported"); err != nil { 77 t.Error(err) 78 } 79 if err := hasOperations(g, "input", "neg", "imported/input", "imported/neg"); err != nil { 80 t.Error(err) 81 } 82 } 83