File size: 1,560 Bytes
ab687e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from pytorch_caney.loss.utils import to_tensor

import unittest
import numpy as np
import torch


class TestToTensorFunction(unittest.TestCase):

    def test_tensor_input(self):
        tensor = torch.tensor([1, 2, 3])
        result = to_tensor(tensor)
        self.assertTrue(torch.equal(result, tensor))

    def test_tensor_input_with_dtype(self):
        tensor = torch.tensor([1, 2, 3])
        result = to_tensor(tensor, dtype=torch.float32)
        self.assertTrue(torch.equal(result, tensor.float()))

    def test_numpy_array_input(self):
        numpy_array = np.array([1, 2, 3])
        expected_tensor = torch.tensor([1, 2, 3])
        result = to_tensor(numpy_array)
        self.assertTrue(torch.equal(result, expected_tensor))

    def test_numpy_array_input_with_dtype(self):
        numpy_array = np.array([1, 2, 3])
        expected_tensor = torch.tensor([1, 2, 3], dtype=torch.float32)
        result = to_tensor(numpy_array, dtype=torch.float32)
        self.assertTrue(torch.equal(result, expected_tensor))

    def test_list_input(self):
        input_list = [1, 2, 3]
        expected_tensor = torch.tensor([1, 2, 3])
        result = to_tensor(input_list)
        self.assertTrue(torch.equal(result, expected_tensor))

    def test_list_input_with_dtype(self):
        input_list = [1, 2, 3]
        expected_tensor = torch.tensor([1, 2, 3], dtype=torch.float32)
        result = to_tensor(input_list, dtype=torch.float32)
        self.assertTrue(torch.equal(result, expected_tensor))


if __name__ == '__main__':
    unittest.main()