# Path: studio/util/gpu_util.py
# def memstr2int(string):
#     conversion_factors = [
#         ('Mb', 2**20), ('MiB', 2**20), ('m', 2**20), ('mb', 2**20),
#         ('Gb', 2**30), ('GiB', 2**30), ('g', 2**30), ('gb', 2**30),
#         ('kb', 2**10), ('k', 2**10)
#     ]
# 
#     for key, factor in conversion_factors:
#         if string.endswith(key):
#             return int(float(string.replace(key, '')) * factor)
# 
#     return int(string)


import unittest
from studio.util.gpu_util import memstr2int


class GpuUtilTest(unittest.TestCase):

    def test_memstr2int(self):
        self.assertEqual(memstr2int('123 Mb'), 123 * (2**20))
        self.assertEqual(memstr2int('456 MiB'), 456 * (2**20))
        self.assertEqual(memstr2int('23 Gb'), 23 * (2**30))
        self.assertEqual(memstr2int('23 GiB'), 23 * (2**30))
        self.assertEqual(memstr2int('23 '), 23)

        with self.assertRaises(ValueError):

# Path: studio/util/gpu_util.py
# def memstr2int(string):
#     conversion_factors = [
#         ('Mb', 2**20), ('MiB', 2**20), ('m', 2**20), ('mb', 2**20),
#         ('Gb', 2**30), ('GiB', 2**30), ('g', 2**30), ('gb', 2**30),
#         ('kb', 2**10), ('k', 2**10)
#     ]
# 
#     for key, factor in conversion_factors:
#         if string.endswith(key):
#             return int(float(string.replace(key, '')) * factor)
# 
#     return int(string)


# Path: tests/gpu_util_test.py
import unittest
from studio.util.gpu_util import memstr2int



class GpuUtilTest(unittest.TestCase):

    def test_memstr2int(self):
        self.assertEqual(memstr2int('123 Mb'), 123 * (2**20))
        self.assertEqual(memstr2int('456 MiB'), 456 * (2**20))
        self.assertEqual(memstr2int('23 Gb'), 23 * (2**30))
        self.assertEqual(memstr2int('23 GiB'), 23 * (2**30))
        self.assertEqual(memstr2int('23 '), 23)

        with self.assertRaises(ValueError):

            memstr2int('300 spartans')