MCPcopy Create free account
hub / github.com/ml-explore/mlx-data / test_passing_python_objects

Method test_passing_python_objects

python/tests/test_buffer.py:45–77  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

43 self.assertEqual(i, e["i"])
44
45 def test_passing_python_objects(self):
46 with self.assertRaises(ValueError):
47 b = dx.buffer_from_vector([{"a": "hello"}])
48 with self.assertRaises(ValueError):
49 b = dx.buffer_from_vector([{"a": object()}])
50
51 x = array.array("f")
52 x.append(10)
53 x.append(-2.5)
54 y = np.random.randn(10)
55 b = dx.buffer_from_vector(
56 [
57 {
58 "a": 1,
59 "b": 1.2,
60 "c": b"Hello world",
61 "d": y,
62 "e": x,
63 }
64 ]
65 )
66 self.assertEqual(-2.5, b[0]["e"][1])
67 self.assertEqual(1, b[0]["a"])
68 self.assertTrue(np.all(y == b[0]["d"]))
69 self.assertTrue(np.all(x == b[0]["e"]))
70
71 # Check that we take np arrays without a copy
72 y[0] = 0
73 self.assertTrue(np.all(y == b[0]["d"]))
74
75 # and buffers via copy
76 x[0] = 0
77 self.assertEqual(10, b[0]["e"][0])
78
79
80if __name__ == "__main__":

Callers

nothing calls this directly

Calls 1

appendMethod · 0.80

Tested by

no test coverage detected