Skip to content

Commit c3b1e5f

Browse files
committed
Add more low-level tests
1 parent 6bb28e9 commit c3b1e5f

1 file changed

Lines changed: 74 additions & 3 deletions

File tree

SampleTest/LowLevelTests.cs

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//
1+
//
22
// Low-level tests
33
//
44
using System;
@@ -24,9 +24,9 @@ TFOperation Placeholder (TFGraph graph, TFStatus s)
2424
return j;
2525
}
2626

27-
TFOperation ScalarConst (int v, TFGraph graph, TFStatus status)
27+
TFOperation ScalarConst (TFTensor v, TFGraph graph, TFStatus status, string name = null)
2828
{
29-
var desc = new TFOperationDesc (graph, "Const", "scalar");
29+
var desc = new TFOperationDesc (graph, "Const", name == null ? "scalar" : name);
3030
desc.SetAttr ("value", v, status);
3131
if (status.StatusCode != TFCode.Ok)
3232
return null;
@@ -295,7 +295,78 @@ public void AttributesTest ()
295295
var op = desc.FinishOperation ();
296296
ExpectMeta (op, "v", 2, TFAttributeType.Shape, 5);
297297
}
298+
}
299+
300+
public void AddControlInput ()
301+
{
302+
Console.WriteLine ("Testing AddControlInput for assertions");
303+
var status = new TFStatus ();
304+
using (var g = new TFGraph ()) {
305+
var s = new TFSession (g, status);
306+
307+
TFTensor yes = true;
308+
TFTensor no = false;
309+
var placeholder = g.Placeholder (TFDataType.Bool, operName: "boolean");
310+
311+
var check = new TFOperationDesc (g, "Assert", "assert")
312+
.AddInput (placeholder)
313+
.AddInputs (placeholder)
314+
.FinishOperation ();
315+
316+
var noop = new TFOperationDesc (g, "NoOp", "noop")
317+
.AddControlInput (check)
318+
.FinishOperation ();
319+
320+
var runner = s.GetRunner ();
321+
runner.AddInput (placeholder, yes);
322+
runner.AddTarget (noop);
323+
324+
// No problems when the Assert check succeeds
325+
runner.Run ();
326+
327+
// Exception thrown by the execution of the Assert node
328+
try {
329+
runner = s.GetRunner ();
330+
runner.AddInput (placeholder, no);
331+
runner.AddTarget (noop);
332+
runner.Run ();
333+
throw new Exception ("This should have thrown an exception");
334+
} catch (Exception e) {
335+
Console.WriteLine ("Success, got the expected exception when using tensorflow control inputs to assert");
336+
}
337+
}
338+
}
298339

340+
public void TestParametersWithIndexes ()
341+
{
342+
Console.WriteLine ("Testing Parameters with indexes");
343+
var status = new TFStatus ();
344+
using (var g = new TFGraph ()) {
345+
var s = new TFSession (g, status);
346+
347+
var split = new TFOperationDesc (g,"Split", "Split")
348+
.AddInput (ScalarConst (0, g, status) [0])
349+
.AddInput (ScalarConst (new TFTensor (new int [] { 1, 2, 3, 4 }), g, status, "array") [0])
350+
.SetAttr ("num_split", 2)
351+
.FinishOperation ();
352+
var add = new TFOperationDesc (g, "Add", "Add")
353+
.AddInput (split [0]).AddInput (split [1]).FinishOperation () [0];
354+
355+
// fetch using colon separated names
356+
var fetched = s.GetRunner ().Fetch ("Split:1").Run () [0];
357+
var vals = fetched.GetValue () as int [];
358+
if (vals [0] != 3 || vals [1] != 4)
359+
throw new Exception ("Expected the values 3 and 4");
360+
361+
// Add inputs using colon separated names.
362+
var t = new TFTensor (new int [] { 4, 3, 2, 1 });
363+
var ret = (s.GetRunner ().AddInput ("Split:0", t).AddInput ("Split:1", t).Fetch ("Add").Run ()).GetValue (0) as TFTensor;
364+
var val = ret.GetValue () as int [];
365+
366+
if (val [0] != 8 || val [1] != 6 || val [2] != 4 || val [3] != 2)
367+
throw new Exception ("Expected 8, 6, 4, 2");
368+
}
369+
Console.WriteLine ("success");
299370
}
300371
}
301372
}

0 commit comments

Comments
 (0)