memory-map-selftests.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* Self tests for memory-map for GDB, the GNU debugger.
  2. Copyright (C) 2017-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "gdbsupport/selftest.h"
  16. #include "memory-map.h"
  17. #if defined(HAVE_LIBEXPAT)
  18. namespace selftests {
  19. namespace memory_map_tests {
  20. /* A simple valid test input for parse_memory_map. */
  21. static const char valid_mem_map[] = R"(<?xml version="1.0"?>
  22. <!DOCTYPE memory-map
  23. PUBLIC "+//IDN gnu.org//DTD GDB Memory Map V1.0//EN"
  24. "http://sourceware.org/gdb/gdb-memory-map.dtd">
  25. <memory-map>
  26. <memory type="ram" start="0" length="4096" />
  27. <memory type="rom" start="65536" length="256" />
  28. <memory type="flash" start="131072" length="65536">
  29. <property name="blocksize">1024</property>
  30. </memory>
  31. </memory-map>
  32. )";
  33. /* Validate memory region R against some expected values (the other
  34. parameters). */
  35. static void
  36. check_mem_region (const mem_region &r, CORE_ADDR lo, CORE_ADDR hi,
  37. mem_access_mode mode, int blocksize)
  38. {
  39. SELF_CHECK (r.lo == lo);
  40. SELF_CHECK (r.hi == hi);
  41. SELF_CHECK (r.enabled_p);
  42. SELF_CHECK (r.attrib.mode == mode);
  43. SELF_CHECK (r.attrib.blocksize == blocksize);
  44. }
  45. /* Test the parse_memory_map function. */
  46. static void
  47. parse_memory_map_tests ()
  48. {
  49. std::vector<mem_region> regions = parse_memory_map (valid_mem_map);
  50. SELF_CHECK (regions.size () == 3);
  51. check_mem_region (regions[0], 0, 0 + 4096, MEM_RW, -1);
  52. check_mem_region (regions[1], 65536, 65536 + 256, MEM_RO, -1);
  53. check_mem_region (regions[2], 131072, 131072 + 65536, MEM_FLASH, 1024);
  54. }
  55. } /* namespace memory_map_tests */
  56. } /* namespace selftests */
  57. #endif /* HAVE_LIBEXPAT */
  58. void _initialize_memory_map_selftests ();
  59. void
  60. _initialize_memory_map_selftests ()
  61. {
  62. #if defined(HAVE_LIBEXPAT)
  63. selftests::register_test
  64. ("parse_memory_map",
  65. selftests::memory_map_tests::parse_memory_map_tests);
  66. #endif
  67. }