You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

StringsApiTest.cs 2.2 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using Tensorflow;
  6. using static Tensorflow.Binding;
  7. namespace TensorFlowNET.UnitTest.TF_API
  8. {
  9. [TestClass]
  10. public class StringsApiTest
  11. {
  12. [TestMethod]
  13. public void StringFromBytes()
  14. {
  15. var jpg = tf.constant(new byte[] { 0x41, 0xff, 0xd8, 0xff }, tf.@string);
  16. var strings = jpg.ToString();
  17. Assert.AreEqual(strings, @"tf.Tensor: shape=(), dtype=string, numpy=A\xff\xd8\xff");
  18. }
  19. [TestMethod]
  20. public void StringEqual()
  21. {
  22. var str1 = tf.constant("Hello1");
  23. var str2 = tf.constant("Hello2");
  24. var result = tf.equal(str1, str2);
  25. Assert.IsFalse(result.ToScalar<bool>());
  26. var str3 = tf.constant("Hello1");
  27. result = tf.equal(str1, str3);
  28. Assert.IsTrue(result.ToScalar<bool>());
  29. var str4 = tf.strings.substr(str1, 0, 5);
  30. var str5 = tf.strings.substr(str2, 0, 5);
  31. result = tf.equal(str4, str5);
  32. Assert.IsTrue(result.ToScalar<bool>());
  33. }
  34. [TestMethod]
  35. public void ImageType()
  36. {
  37. var imgPath = TestHelper.GetFullPathFromDataDir("shasta-daisy.jpg");
  38. var contents = tf.io.read_file(imgPath);
  39. var substr = tf.strings.substr(contents, 0, 3);
  40. var jpg = tf.constant(new byte[] { 0xff, 0xd8, 0xff }, tf.@string);
  41. var result = math_ops.equal(substr, jpg);
  42. Assert.IsTrue((bool)result);
  43. }
  44. [TestMethod]
  45. public void StringArray()
  46. {
  47. var strings = new[] { "map_and_batch_fusion", "noop_elimination", "shuffle_and_repeat_fusion" };
  48. var tensor = tf.constant(strings, dtype: tf.@string, name: "optimizations");
  49. var stringData = tensor.StringData();
  50. Assert.AreEqual(3, tensor.shape[0]);
  51. Assert.AreEqual(strings[0], stringData[0]);
  52. Assert.AreEqual(strings[1], stringData[1]);
  53. Assert.AreEqual(strings[2], stringData[2]);
  54. }
  55. }
  56. }