ProteoWizard
endian_test.cpp
Go to the documentation of this file.
1//
2// $Id$
3//
4//
5// Original author: Darren Kessner <darren@proteowizard.org>
6//
7// Copyright 2007 Spielberg Family Center for Applied Proteomics
8// Cedars-Sinai Medical Center, Los Angeles, California 90048
9//
10// Licensed under the Apache License, Version 2.0 (the "License");
11// you may not use this file except in compliance with the License.
12// You may obtain a copy of the License at
13//
14// http://www.apache.org/licenses/LICENSE-2.0
15//
16// Unless required by applicable law or agreed to in writing, software
17// distributed under the License is distributed on an "AS IS" BASIS,
18// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19// See the License for the specific language governing permissions and
20// limitations under the License.
21//
22
23
24#include "Std.hpp"
25#include "endian.hpp"
27
28using namespace pwiz::util;
29
30
31void test()
32{
33 unsigned char bytes[] = {0xce, 0xfa, 0xca, 0xca, 0xfe, 0xca, 0x20, 0x04};
34 unsigned int n = *reinterpret_cast<unsigned int*>(bytes);
35
36 #if defined(PWIZ_LITTLE_ENDIAN)
37 unit_assert(n == 0xcacaface);
38 unit_assert(endianize32(n) == 0xcefacaca);
39 #elif defined(PWIZ_BIG_ENDIAN)
40 unit_assert(n == 0xcefacaca);
41 unit_assert(endianize32(n) == 0xcacaface);
42 #endif
43
44 unsigned long long m = *reinterpret_cast<unsigned long long*>(bytes);
45
46 #if defined(PWIZ_LITTLE_ENDIAN)
47 unit_assert(m == 0x420cafecacafacell);
48 unit_assert(endianize64(m) == 0xcefacacafeca2004ll);
49 #elif defined(PWIZ_BIG_ENDIAN)
50 unit_assert(m == 0xcefacacafeca2004ll);
51 unit_assert(endianize64(m) == 0x420cafecacafacell);
52 #endif
53}
54
55
56int main(int argc, char* argv[])
57{
58 TEST_PROLOG(argc, argv)
59
60 try
61 {
62 test();
63 }
64 catch (exception& e)
65 {
66 TEST_FAILED(e.what())
67 }
68 catch (...)
69 {
70 TEST_FAILED("Caught unknown exception.")
71 }
72
74}
75
76
int main(int argc, char *argv[])
void test()
unsigned long long endianize64(unsigned long long n)
Definition endian.hpp:80
unsigned int endianize32(unsigned int n)
Definition endian.hpp:74
#define unit_assert(x)
Definition unit.hpp:85
#define TEST_EPILOG
Definition unit.hpp:183
#define TEST_FAILED(x)
Definition unit.hpp:177
#define TEST_PROLOG(argc, argv)
Definition unit.hpp:175