static void test_UInt32ToStrBase() { const size_t max = 32 + 1; uint32_t val[] = {0, 1, -1, INT32_MIN, INT32_MAX, 0x01234567, 0x89abcdef}; int N = sizeof (val) / sizeof (uint32_t); int i; char str[max]; char ref[max]; size_t len; // test conversion to decimal numbers for (i = 0; i < N; i++) { len = SCPI_UInt32ToStrBase(val[i], str, max, 10); snprintf(ref, max, "%"PRIu32, val[i]); CU_ASSERT(len == strlen(ref)); CU_ASSERT_STRING_EQUAL(str, ref); } // test conversion to hexadecimal numbers for (i = 0; i < N; i++) { len = SCPI_UInt32ToStrBase(val[i], str, max, 16); snprintf(ref, max, "%"PRIX32, val[i]); CU_ASSERT(len == strlen(ref)); CU_ASSERT_STRING_EQUAL(str, ref); } // test conversion to octal numbers for (i = 0; i < N; i++) { len = SCPI_UInt32ToStrBase(val[i], str, max, 8); snprintf(ref, max, "%"PRIo32, val[i]); CU_ASSERT(len == strlen(ref)); CU_ASSERT_STRING_EQUAL(str, ref); } // test conversion to binary numbers len = SCPI_UInt32ToStrBase(0, str, max, 2); CU_ASSERT(len == 1); CU_ASSERT_STRING_EQUAL(str, "0"); len = SCPI_UInt32ToStrBase(1, str, max, 2); CU_ASSERT(len == 1); CU_ASSERT_STRING_EQUAL(str, "1"); len = SCPI_UInt32ToStrBase(-1, str, max, 2); CU_ASSERT(len == 32); CU_ASSERT_STRING_EQUAL(str, "11111111111111111111111111111111"); len = SCPI_UInt32ToStrBase(0x01234567, str, max, 2); CU_ASSERT(len == 25); CU_ASSERT_STRING_EQUAL(str, "1001000110100010101100111"); len = SCPI_UInt32ToStrBase(0x89abcdef, str, max, 2); CU_ASSERT(len == 32); CU_ASSERT_STRING_EQUAL(str, "10001001101010111100110111101111"); }
/** * Write arbitrary block header with length * @param context * @param len * @return */ size_t SCPI_ResultArbitraryBlockHeader(scpi_t * context, size_t len) { char block_header[12]; size_t header_len; block_header[0] = '#'; SCPI_UInt32ToStrBase((uint32_t) len, block_header + 2, 10, 10); header_len = strlen(block_header + 2); block_header[1] = (char) (header_len + '0'); context->arbitrary_reminding = len; return writeData(context, block_header, header_len + 2); }
/** * Write arbitrary block program data to the result * @param context * @param data * @param len * @return */ size_t SCPI_ResultArbitraryBlock(scpi_t * context, const char * data, size_t len) { size_t result = 0; char block_header[12]; size_t header_len; block_header[0] = '#'; SCPI_UInt32ToStrBase((uint32_t) len, block_header + 2, 10, 10); header_len = strlen(block_header + 2); block_header[1] = (char)(header_len + '0'); result += writeData(context, block_header, header_len + 2); result += writeData(context, data, len); context->output_count++; return result; }